I can’t seem to wrap my head around the differences between the following in jQuery Mobile:
$( document ).live('pageshow', function(event) {
});
$( document ).bind('pageshow', function(event) {
});
$( document ).delegate("#page", "pageshow", function() {
});
How do I execute scripts in the head of my documents that are DIFFERENT in certain pages? Which methods do I use to call those scripts?
Update:
jQuery version: 1.7.1
jQuery Mobile version: 1.1.0
You would bind to a “page event” that jQuery Mobile exposes, like
pageinit:Since you’re using jQuery Core 1.7.1 you can use
.on()which has a slightly different syntax:All three of your methods do similar things.
.live()is the same thing as using.delegate()withdocumentas the root selection but it’s been depreciated so it’s a good idea to stop using it (source: http://api.jquery.com/on). Using.bind()directly on thedocumentelement is the same as using.delegate()but when you use.bind()you have to determine which pseudo-page had the event fired on it in the event handler rather than in the function call.For example:
In general, event delegation is used when we don’t know when an element will exist in the DOM. It relies on events bubbling up the DOM until they get to the root selection (in your case it’s always the
documentelement).Docs for
.delegate(): http://api.jquery.com/delegateFor more general information about the difference between these functions, see this article (I’ve read it to check it for accuracy and it’s right-on): http://www.elijahmanor.com/2012/02/differences-between-jquery-bind-vs-live.html