我在试着理解我的代码里发生了什么。
所以我所做的是1.iframe在一个页面中指向带有一些javascript的aspx页面。值得注意的是,它的相同的领域,所以没有问题的访问。2. iframe窗口中的代码获取父文档对象,并将所需的内容注入到配置到位的父头中,以加载my-custom-code.js 3。因此,my-custom-code.js被加载到父页面的头部并运行良好。
!!!这就是我不明白的!当我尝试在我的-定制-code.js中使用窗口和文档时,它们实际上指向子文档(Iframe)和窗口?
所以我想了解它是如何工作的。
谢谢
更新: iframe文件
var $doc = $(parent.document);
var head = $doc.find("head");
var body = $doc.find("body");
var reqConfigScript = "<script class='require-default-config'>var require = { deps: ['http:\/\/localhost/scripts/parent-main.js'], callback: function(main){ } };</script>";
body.append(reqConfigScript);
var parentScript = "<script src='http:\/\/" + location.host + "/scripts/require-2.1.11.min.js' type=\"text/javascript\"></script>";
head.append(parentScript);发布于 2014-02-25 16:24:48
好吧,这有点难猜,因为我不知道图书馆是怎么运作的.但是试试这个:
parent.require = {
deps: ['http:\/\/localhost/scripts/parent-main.js'],
callback: function(main){ }
};
var parentScript = parent.document.createElement('script');
// important to use `parent.document` so the right document owns the script
parentScript.src = "http://"+location.host+"/script/require-2.1.11.min.js";
parentScript.type = "text/javascript";
parent.document.body.appendChild(parentScript);https://stackoverflow.com/questions/22019713
复制相似问题