Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running javascript after page has been loaded [duplicate]

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);
like image 360
Masriyah Avatar asked Apr 05 '13 17:04

Masriyah


People also ask

What are the ways to execute JavaScript after page load?

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.

Which event would you use to start a script after a page has been fully loaded by the browser?

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.).

How do I run a function after page load?

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.


1 Answers

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');
    });    
});
like image 193
dsgriffin Avatar answered Sep 29 '22 07:09

dsgriffin