I currently have a javascript file that i call in my view page in which i get my data information to display. At the same time i have this piece of javascript that shows a menu bar once the image has been clicked. This scripted is being ran before the page is loaded completely or something because it is not working. How can i make a piece of my javascript take action after all other scripts have completed running and page is loaded? this is what i tried but not working right
Javascript
(function ($) {
.... lines to load view page with data
$(document).ready(function () {
$('figure').on('click', function (event) {
$(event.currentTarget).toggleClass('open');
});
});
})(jQuery);
Method 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs whenever the element has finished loading. This can be used with the body element to execute a script after the webpage has completely loaded.
The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).
The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.
You could use $(window).on("load")
instead of $(document).ready
if you want to wait until all the elements have loaded in that case:
$(window).on("load", function(){
$('figure').on('click', function (event) {
$(event.currentTarget).toggleClass('open');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With