最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Load HTMLJavascript dynamically - Stack Overflow

programmeradmin1浏览0评论

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 

});

});

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 

});

});
Share Improve this question asked Jun 18, 2011 at 17:38 Le_CoeurLe_Coeur 1,5415 gold badges28 silver badges48 bronze badges 1
  • Have you tried loading the scripts before loading the html? – Yet Another Geek Commented Jun 18, 2011 at 17:40
Add a ment  | 

1 Answer 1

Reset to default 10

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)

  1. 暂无评论