Hello to all! This is the problem with javascript I have a file where there is

jQuery(document).ready(function(){}); 

and there is a main file, that is, I need a php javascript file there due to the fact that I insert php code into the javascript part. and both scripts have jQuery(document).ready(function(){}); and one of them does not work that which after. I do not know how people decide help

  • This is what you have written: jQuery (document) .ready (function () {}); Or is there still a function body? Then lay out all the code. Maybe in the second function there is an error. - Oleg Arkhipov
  • what is this nonsense ??? - MaximPro
  • four
    There is a mistake. jQuery(document).ready(function(){}) installs a ready handler, in which it is once again set to jQuery(function(){}) . - KiTE
  • so what should I do? - FreelancerUz
  • @KiTE is right, it turns out recursion and the browser blocks its execution (in theory) - Crasher

2 answers 2

Better through PHP to generate the filling for the container type:

 <script id="php_data" type="text/x-php-data"> </script> 

And through JQuery to read it and parse. For example:

 jQuery(document).ready(function(){ var data = jQuery('#php_data').html(); }); 
  • This trash code would be a reputation would make a minus - FreelancerUz
  • Yes?!! @FreelancerUz, and, I believe that bydlokod is when PHP-shny inserts are molded into JavaScript, and, thus, block the possibility of rendering the JavaScript-logic of the page into a separate static file. Or still they endure everything separately, and then create another ready-handler in HTML ... By the way, it turns out that bylolockers work for Microsoft. It was there that the [jQuery Templates] [1] plugin was developed, which proposes to separate the logic from the variable content. A variable content stored in containers <script> with an unknown for the browser type . [1]: github.com/jquery/jquery-tmpl - KiTE
  • FreelancerUz, judging by the structure, your bydlokod bydlokodeee @KITE code in 9,000 times;) - DarkFighter

In the body of the second script (not working):

 <script type="text/javascript"> jQuery(document).ready(function(){ // Слайдер Фотографии jQuery(function(){ jQuery(".photos").jCarouselLite({ btnPrev: ".photo-next", btnNext: ".photo-prev", visible: <?=$photos_count?>, auto: 5000, speed: 700, vertical: true, }); }); }); </script> 

So. We will deal with the code. jQuery(document).ready(function(){}) sets the function to run after the DOM is loaded. jQuery(function(){}) does the same thing. Since the DOM load event has already happened, it is too late to hang up a function on it. Therefore, remove the second "attachment" and perform the function immediately.