I want to trigger one event on page load complete using javascript/jquery.
Is there any way to trigger event or call a simple function once page loading fully completes.
Please suggest folks if you any reference.
$("document"). ready(function() { $("ul. galleria li:first-child img"). trigger('click'); });
Method 1: Using the on() method with the load event: The on() method in jQuery is used to attach an event handler for any event to the selected elements. The window object is first selected using a selector and the on() method is used on this element.
Yeah, you can use a click event called onLoad() . Just use the setTimeout() method in jquery. It will call a click function without clicking it.
Everyone's mentioned the ready
function (and its shortcuts), but even earlier than that, you can just put code in a script
tag just before the closing body
tag (this is what the YUI and Google Closure folks recommend), like this:
<script type='text/javascript'> pageLoad(); </script> </body>
At this point, everything above that script tag is available in the DOM.
So your options in order of occurrence:
Earliest: Function call in script
tag just before closing the body
tag. The DOM is ready at this point (according to the Google Closure folks, and they should know; I've also tested it on a bunch of browsers).
Earlyish: the jQuery.ready
callback (and its shortcut forms).
Late, after all page elements including images are fully loaded: window
onload
event.
Here's a live example: http://jsbin.com/icazi4, relevant extract:
</body> <script type='text/javascript'> runPage(); jQuery(function() { display("From <tt>jQuery.ready</tt> callback."); }); $(window).load(function() { display("From <tt>window.onload</tt> callback."); }); function runPage() { display("From function call at end of <tt>body</tt> tag."); } function display(msg) { var p = document.createElement('p'); p.innerHTML = msg; document.body.appendChild(p); } </script>
(Yes, I could have used jQuery for the display
function, but I was starting with a non-jQuery template.)
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