简体   繁体   English

单独的Javascript,iframe /父级

[英]separate Javascript, iframe/parent

I have a big flaw in my idea and was wondering if anyone could help me. 我的想法有一个很大的缺陷,想知道是否有人可以帮助我。 I have a script which I have written that runs on a page nicely. 我有一个编写的脚本,可以很好地在页面上运行。 on that page there is an iframe, from which it's content is from a temporary web page ( temporary / dynamically generated for preview purposes ) which is in full working order ( ie it runs all it's Javascript / server side code as it would normally ). 在该页面上有一个iframe,其内容来自处于完全工作状态的临时网页(临时/为预览目的而动态/动态生成)(即,它像往常一样运行所有Javascript /服务器端代码)。 I have no control on what content is being loaded via the iframe so my problem is this. 我无法控制通过iframe加载什么内容,所以我的问题是这样。 if there is an error within the Javascript code from the iframe ( this mainly applies to Internet Explorer ) then it stops the code from the parent running. 如果来自iframe的Javascript代码内有错误(这主要适用于Internet Explorer),则它将停止运行父代码。 Meaning I'm relying on people to be using correctly coded Javascripts for them to be able to use my service. 意味着我依靠人们使用正确编码的Javascript来使他们能够使用我的服务。

Is there anyway to stop the iframe stopping the parents code working? 无论如何,有没有停止iframe阻止父母代码正常工作的方法?

try {
    document.getElementById('iframe').src = 'iframe.html';
} catch (e) {
    //!!!
}

You have to programmatically set the content in the iframe using javascript and do it in a try/catch block. 您必须使用javascript以编程方式设置iframe中的内容,然后在try / catch块中进行设置。 So that any errors in the iframe will be caught and your script has effect on it. 这样一来,iframe中的任何错误都将被捕获,并且脚本会对它产生影响。

See this post for more details 详情请参阅这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM