开发者

Load HTML/Javascript dynamically

开发者 https://www.devze.com 2023-03-13 17:58 出处:网络
I have an index.html page and I want to load the content of another page to index.html dynamically using jQuery. I use $.load() function to do this :

I have an index.html page and I want to load the content of another page to index.html dynamically using jQuery. I use $.load() function to do this :

$('#dynamic_content').load('another.html #toLoad')

It works fine, but I need also to load javascript files that uses another.html, so I do:

$('#dynamic_content').load开发者_高级运维('another.html #toLoad');
$.getScript('js/another.js');

The problem is that the js code of 'another.js' sometimes doesn't 'apply' to html page(maybe it loads earlier than the html page) Content of another.js:

$(document).ready(function {} {

   $('#cancelButton').click(function() {
       //do something 

});

});


Use the success callback:

$('#dynamic_content').load('another.html #toLoad', function() {
    $.getScript('js/another.js');
});

This way if another.js manipulates the dynamically loaded contents from another.html it will be guaranteed that this content has already been injected into the current DOM tree.

0

精彩评论

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

关注公众号