JQuery (document) .ready () doesn't work

Does anyone know why a finished jQuery document might not work on a website? I put this particular script in the footer and it just doesn't work (jQuery 1.8 is included in the chapter section):

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

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

 jQuery("#slideshow iframe").each(function(){ alert('test'); }); 
+4
source share
3 answers

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 your flow.anything-slider-1.0.js on line 11 .

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

Changing line 11 with $ 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 to one way to use jQuery, and not to mix it.

Edit
I just double-checked the .ready () documentation, and the next paragraph was interesting as it seems to be related to the problem

JQuery namespace merge
When using a different JavaScript library, we can call $.noConflict() to avoid namespace problems. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery every time we usually write $ .

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

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

This would mean that instead of committing line 11 you can also change your fist line to jQuery(document).ready(function($) { by passing $ as an argument. This can allow you to use $ for the whole file as well as for jQuery .

In any case, I'm not sure if passing $ as an argument will work in your case, I just thought that I would mention this if it really works.

+3
source

This is not the case in this particular case, but the very dangerous way this can happen is with jQuery error 10251 : an error in the $(document).ready() callback will prevent all callbacks registered after it starts. (In this case, there will be a Javascript error, but it may look completely unrelated.)

+5
source

This may be a problem with the jQuery library. Try downloading it from jquery.com -

 <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script type="text/javascript"> $(document).ready(function() { alert('test'); }); </script> 

Copy this code and paste in the head section

Edit:

You are using jQuery 1.4.2 and 1.8 jQuery UI

Try moving the script tag section to head , it should work

+1
source

Source: https://habr.com/ru/post/1435648/


All Articles