Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
58
rated 0 times [  62] [ 4]  / answers: 1 / hits: 26763  / 12 Years ago, sat, september 22, 2012, 12:00:00

Does anyone know why jQuery document ready might not fire on a website? I put exactly this script in footer and it simply doesn't fire (jQuery 1.8 is included in head section):



<script type=text/javascript>
jQuery(document).ready(function() {
alert('test');
jQuery(#slideshow iframe).each(function(){
alert('test');
});
});
</script>


There are no Javascript errors, console is empty and when I run this in Firebug's console it works:



jQuery(#slideshow iframe).each(function(){ 
alert('test');
});

More From » jquery

 Answers
70

You are currently getting this error on your page



Uncaught TypeError: Property '$' of object [object Window] is not a function 


The cause of this error is inside your flow.anything-slider-1.0.js at line 11.



The file is using jQuery(document).ready(), so $ is not defined.



Changing line 11 from using $ to jQuery works:



// doesn't work
$(#content).before(<div id=cycledump></div>);

// Does work
jQuery(#content).before(<div id=cycledump></div>);


The whole file uses jQuery instead of $ so the file should probably stick with the one way of using jQuery instead of mixing it up.



Edit

I just double checked the .ready() documentation and the following paragraph was interesting as it seems to relate to the issue:




Aliasing the jQuery Namespace

When using another JavaScript library, we may wish to call $.noConflict() to avoid namespace difficulties. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery each time we would normally write $.



However, the handler passed to the .ready() method can take an argument, which is passed to the global jQuery object. This means we can rename the object within the context of our .ready() handler without affecting other code:



jQuery(document).ready(function($) {
// Code using $ as usual goes here.
});



This would imply, that instead of fixing line 11 you could also change your fist line to jQuery(document).ready(function($) {, passing the $ as an argument. This might allow you then to use $ throughout the file as well as jQuery.



Anyway, not sure passing $ as an argument would work in your case, I just thought I mention it in case it does work.


[#82957] Thursday, September 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kiarra

Total Points: 202
Total Questions: 111
Total Answers: 109

Location: Sudan
Member since Mon, May 31, 2021
3 Years ago
;