Tell me, please, why such a construction refuses to work.

<body> <div id="content"> </div> </body> <script> $(function() { $('#content').load('../include/index.php'); }) </script> 

The problem is that if the content from the index.php file is simply inserted into id = "content", then the jQuery methods work well, and if the content is loaded with the .load method, then many functions stop working. I know that using the method .live events retain their performance.

For example:

 $('button').live('click', function() { //Действие }) 

But what about the functions, especially when there are a lot of them? Of course, you can load the scripts together with the loadable file, but it will not be correct, why load the scripts each time.

I just use the jQuery cookie plugin and after downloading the content via the .load the cookie plugin no longer works.

Tell me the solution to the problem.

  • The handlers you are talking about are hung up by loading the main page? - Gedweb
  • one
    .load() intended not to load pages entirely, but to update some data without special gestures, a block of news there or a set of banners. * keep the scripts in the .js files load them through <script src= , if you need to load the script, read $.getScript - there is an example of how to make a caching loader. - zb '11

2 answers 2

To begin with, it would be time to forget about the live () method, it was written hundreds of times. Use the .on () method. Secondly, when working with dynamically loaded content, I would recommend delegated processing . For example, if your main container does not change, then you hang up an event starting from it.

 $('#container').on('click', '.elem', function(e){ // ... }); 

In this case, the internal code of this container is re-indexed and the necessary element is located. It is also not necessary to abuse this, because All these movements affect the speed. And try to find the closest parent element, and not the body, for example.

  • > In this case, after loading the page to the downloaded content via .load, the scripts do not respond. Would you like to say that you are uploading not a part of the content, but the whole page? If not, content uploading should not affect the operation of the jquery.cookie.js plugin. Also, why not use LocalStorage? Or religion does not allow? )) - Deonis
  • True, only part of the content is loaded in my browser, but for some reason jquery.cookie.js does not work. If the whole page were loaded, there would be no such problems as the scripts would be loaded with the page. Thanks for LocalStorage. Tell me if anyone knows how to at least form a google request for this topic in order to scoop more information. - webkostya
  • @Berserker, request for your problem? So the problem, I'm pretty sure, is of a private nature. Under the link that you gave above, it is difficult to understand something. Lay out, albeit with errors, but the working version, where you have to load the page, then you can somehow enter the big picture. While I can say that for a start, you need to remove the entire js-code (like onclick) from html and transfer everything to the js-file: " Cutlets separately, flies separately ." Hang up event handlers on the links, wrapping everything in a DOMReady construct. - Deonis
  • Here is the link mywork.zz.mu/metro here in the js / applications.js file which is at the bottom of the html page is worth the .load that loads the content in id = "metro". At the moment, when moving the blocks, the coockies do not remain in sync, but if you don’t load the content, but simply insert it into id = "metro", then the coockie works. - webkostya
  • @Berserker, well, the execution order for you in this case: first try to install / check cookies ( jquery.sortable.js file), and only then load the content ( applications.js file). Here, try loading the content in the jquery.sortable.js file in the place that I showed below. $ (function () {// here, we make the items $ (setSelector) .load ('include / index.php'); // in this place (!!!) $ (setSelector). sortable ({// .... - Deonis

Damn you would not believe but the decision was so

 $(setSelector).load('../include/index.php', function() { restoreOrder(); }); 
  • @Berserker, Why not believe it? )) Everything is logical - after loading the content, call the function in the callback. - Deonis
  • Thanks for directing my gyrus in the right direction))) - webkostya