I have this script
<script type="text/javascript" src="customJS/jsonRetrieve.js" async></script>
The script makes me retrieve the data from a JSON file and store it in an array.
first question is: how can i load my index page only after the script is fully loaded? i get the error of 'undefined'.
After loading the script i have to add the code from the array something like this
"<img src='images2/1.jpg' id='0' style='width:70%' class='rounded' data-toggle='modal' onclick='setMod(this.id);setData(this.id)' data-target='#myModal' />"
to my carousel and that by adding it to the div. I used the jQuery function 'append' to add the images to my #carousel div but the images are not showing in the carousel preview. how can i fix that ? should i refresh the carousel ?
<script type="text/javascript" src="js/jquery.waterwheelCarousel.js" defer></script>
<script type="text/javascript" src="customJS/carouselJS.js" defer></script>
$(window).load(function () {
$('#carousel').append(items[0]);
});
-
1Can you provide a minimal verifiable example with a working snippet?Calvin Nunes– Calvin Nunes2018年05月09日 17:35:49 +00:00Commented May 9, 2018 at 17:35
-
You would ideally need to refresh the carousel after adding the images dynamically.Studocwho– Studocwho2018年05月09日 17:35:50 +00:00Commented May 9, 2018 at 17:35
-
@Studocwho how can i refresh the carousel scripts and css and the divMed yacine– Med yacine2018年05月09日 17:45:06 +00:00Commented May 9, 2018 at 17:45
-
@CalvinNunes thats alot of work x)Med yacine– Med yacine2018年05月09日 17:45:19 +00:00Commented May 9, 2018 at 17:45
-
cant i make the carousel to load only after i added the images dynamically ?Med yacine– Med yacine2018年05月09日 17:47:44 +00:00Commented May 9, 2018 at 17:47
1 Answer 1
You can prevent a script from loading until you add the images with something like this:
<script onload="customJsFunction();" src ="customJS/jsonRetrieve.js"></script>
A similar question is here: Call javascript function after script is loaded.
4 Comments
$('#carousel').append(items[0]);. items isn't an array defined in that function! You need to return the value of jsonRetrieve.js not just include the file. Please check out line 43 jsfiddle.net/j4pruxs5/1