开发者

jQuery: Adding javascript to page via Ajax but does not execute

I use jQuery to make an ajax request and add the code to my page. The code that I add to the page not only contains HTML, but also javascript.

Apparently the javascript-code is not being called! How can I force to my newly added javascript sourcecode be开发者_如何学Going executed?


You probably are using the jQuery(element).load(url) method.

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as , , or elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

Try to scope the content that you are receiving from the server, for example:

// will load only the content inside the element with id="container"
jQuery(element).load("yoururl #container"); 

You probably should try to execute the JS code from the script, using the complete callback, for example:

jQuery(element).load("yoururl #container", function(response, status, xhr) {
    do_stuff_after_load();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜