Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery page load

Tags:

jquery

I see people using all these different techniques with jQuery. I know the 2nd technique will run on page load. But when will the 1st and the 3rd functions fire? The third technique is used in plugins to avoid conflict right? But that will fire before page load surely? I also added a 4th technique. I would like to know when you should/shouldn't use each technique. And if any of them are bogus let me know!

1st

(function($) {

})(jQuery);

2nd

$(document).ready(function(){

});

3rd

$(function(){

}());

4th

jQuery(function($) { 

}); 

5th

(function(){

})();
like image 798
superlogical Avatar asked Feb 09 '10 20:02

superlogical


People also ask

What is the page load event in jQuery?

The load event occurs when a specified element has been loaded. This event works with elements associated with a URL (image, script, frame, iframe), and the window object. Depending on the browser, the load event may not trigger if the image is cached (Firefox and IE).

How check page is loaded or not in jQuery?

Basically, the most reliable way to check if jQuery has been loaded is to check typeof jQuery — it will return "function" if jQuery was already loaded on the page, or "undefined" if jQuery hasn't been loaded yet.

What is window load in jQuery?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

How do you load a page in JavaScript?

Approach: We can use window. location property inside the script tag to forcefully load another page in Javascript. It is a reference to a Location object that is it represents the current location of the document. We can change the URL of a window by accessing it.


2 Answers

Update He's changed the list of calls in the question, so I'm updating to match.

The first is a hack to avoid conflicts with other libraries which might assign $. It is not a ready handler. The second and third are ready event handlers.

From the jQuery API reference:

All three of the following syntaxes are equivalent:

  • $(document).ready(handler)
  • $().ready(handler) (this is not recommended)
  • $(handler)

So although these three do the same thing, avoid the second.

In jQuery 1.3, $() was equal to $(document). This is no longer true in 1.4.

The fourth looks like a syntax error to me. It essentially assigns a new ready handler, but it passes a function with an argument called $. Since this is an event handler, jQuery will pass event info in the first argument. You don't typically want $ to represent event info.

The fifth defines a new function and then invokes it immediately, passing no arguments. So this:

(function(){
    alert("Hi!");
})();

Means the same as this:

alert("Hi!");
like image 113
Craig Stuntz Avatar answered Sep 28 '22 03:09

Craig Stuntz


1st - assigns the $ to jQuery for use only inside the brackets (no conflict version) 2nd is normaln version and 3rd is shorthand :)

more: http://docs.jquery.com/Using_jQuery_with_Other_Libraries#Overriding_the_.24-function

Edit: Oh yes, the shorthand is:

$(function() {
  // your code
});

(no parenthesis after function() brackets)

Edit: After longer reading my own link, it seems, that the first one may only wrap part of code, where $ symbol is assigned to jQuery, but you still have to use $(document).ready()

Edit:As for updated list: 5th type: check the other answer, I did not know about that type.

jQuery(function($){

});

The 4th, however, is used for: 1., attach $ to jQuery, temporarily overloading (inside brackets) any framework, that has $ shorthand in use globaly (i.e. prototype) and ALSO is document.ready statement.

like image 26
Adam Kiss Avatar answered Sep 28 '22 02:09

Adam Kiss