开发者

Javascript - emulating document.ready() on HTML object

I have a main site A onto which I load content with AJAX from sub-site B. There is a jQuery file that alters elements on the site A (adds events to them, classes, re-renders them if needed etc) that is being launched on document.ready()

Now I need to load content from site B into site A. I do it with a function:

$.get("/some/url/",{},function(e){
    $(".some_div").html(e);
});

The problem is if I inclu开发者_高级运维de jQuery in site B, the following happens: jQuery loads the content, puts it into site A and then triggers all the scripts that were fetched within it. Which causes re-render of the site and whole lot of mess.

What I need to do is to emulate the document.ready() on the HTML object e right after it was pulled out of the site B but before it is appended to the site A (so I will have re-rendered HTML code with all events, classes and listeners ready).


Can't you just put your code in the function, and run your stuff before calling:

$(".some_div").html(e);

Just above that line all the html from b is ready but not loaded or run. Or do you mean you want to put in the html and not the scripts? Just use the jquery function to strip out script tags (do e.find('script').appendTo(new_div);).

Or add some code in b. Make a function called init() in b that calls everything. Everything else is in functions and nothing runs. When you are ready call init().


So after some research and time spent on this problem I've found a solution.

In jQuery 1.4 there was a method .live() introduced. The problem is it works only on direct user interactions with UI (i.e. click(), hover() methods etc.). It wont work in methods such as .each() and it wont work with methods that are introduced by third party plugins (such as .accordion(), .tabs(), .datatable() etc.

So right when I decided to extend .live() method on my own I found a wonderful library called livequery. Documentation to it is really good so you shouldn't have any problems figuring out how it works. It solved my problem and thus the case is closed.

(Posting it here so if someone stumbles across similar problem he will find an answer)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜