Four options for you:
Option 0
Read the answer of Sean Hogan , he noted that this can be done with the delegation ( doh! ), And I put together an example implementation.
Option 1
You can href "#" links before loading jQuery, and then (if necessary) change it to what it really should be. You can save what should be href in the data- attribute, for example:
<a href='javascript:;' data-href='realtarget.html'>blah</a>
Then:
jQuery(function($) { $("a[data-href]").each(function() { var $this = $(this); $this.attr("href", $this.attr("data-href")); }); });
Again, the last bit, only if you really need to do href href. If you are processing a click on JavaScript, not necessary.
If validation is an important part of the development cycle, it is important to note that attributes in the form of data-xyz are invalid in HTML4 and earlier (browsers really don't care, but again, if you use validation ...). They become valid with HTML5.
Option 2
Use the onclick built-in attributes to intercept a click before loading jQuery and basically say "Sorry, one moment." So, in the script tag to the top of your file:
function sorryLoading() { alert("Sorry, the page is still loading, one moment please."); return false; }
... and then follow the links:
<a href='realtarget.html' class='sorry' onclick='return sorryLoading();'>blah</a>
... then remove onclick when jQuery loads:
jQuery(function($) { $("a.sorry").removeClass('sorry').attr("onclick", "").click(yourRealClickHandler); });
(You can leave the last bit - click call - off, if they do not all have the same click handler.)
I used the class above to distinguish between these links and others that may have a built-in onclick (otherwise it is surprising that the "[onclick]" selector works on Chrome, Opera and Firefox for Linux and IE6 and IE8 on Windows).
Option 3
Since it seems like you want the page to be dysfunctional before loading jQuery, here is another approach:
- Make sure the script tag for jQuery is in the
head section of your page (and not at the bottom, where I usually recommend placing it). - At the very bottom of your page, before closing the
body tag, jQuery.ready click handlers without jQuery.ready them in a jQuery.ready call (or any of its shortcuts).
The reason is this: # 1 will guarantee that jQuery itself will be loaded to the displayed page, and # 2 will connect handlers as soon as possible. Google recommends using the tag at the end of this page (rather than the ready function or similar) and says that at this point the DOM elements will be ready to be detected.
Separately, of course, you want to make sure that the time during which the links do not do what the user expects is as short as possible , you can perform all the functions of optimizing page loading. By clicking on the link and not having it, it looks as if it is making a bad user interface.