douben8492 2014-08-20 13:38
浏览 203

如何在动态加载的HTML中执行Javascript

I have a webpage in which I dynamically change the HTML in the contentEditor div, depending on what topic the user picks in the navigation menu. The HTML in this contentEditor div gets loaded from external scripts:

function updateContentEditor() {
var window = document.getElementById('contentEditor');

if(editingPage == "Homepagina") {       
    $("#contentEditor").load("http://www.imaginedigital.nl/CMS/Editor/Homepage9.html");
new jscolor.color($line.find('input[name=color]')[0], {});
}
/* and so on... */
}

Now this works perfectly fine, the correct html gets loaded for every page. I do have a problem with Javascript though..

One of the textfields in the script that I have should be a color picker: enter image description here The color picker works perfectly when I load the page itself in my browser, but when I load this page in my contentEditor div, it does not get loaded:

enter image description here

The color picker is an external Javascript (downloaded from: http://jscolor.com/download.php).

I already tried loading it in both the main page as the editor page (editor is the one I load inside the main page), but that won't solve the problem either.

Question How do I make sure my Javascript gets executed when I load a page containing that script inside another HTML page's div?

PLEASE NOTE I do have other javascript in the editor that I load inside my main page. that is javascript that shows a pop up div. that works without any problem.

Any help would be highly appreciated!

  • 写回答

2条回答 默认 最新

  • drhgzx4727 2014-08-20 13:42
    关注

    Have you tried using window.onload=function(){SomeJavaScriptCode}; in the script you are loading?.

    Take a look at this, the beforeload is what you might be looking for:

    How do you detect that a script was loaded *and* executed in a chrome extension?

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧