javascript 无法从释放的脚本中执行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8408358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:21:24  来源:igfitidea点击:

Can't execute code from a freed script

javascriptoverriding

提问by RMD

I have a function that in a different frame that I need to override. In addition, I need to call the original function from within my override. To do so, I'm using the following:

我有一个在不同框架中需要覆盖的函数。此外,我需要从我的覆盖中调用原始函数。为此,我正在使用以下内容:

myFrame.SomeFunction = (function () {
    var originalSomeFunction = myFrame.SomeFunction;

    return function(arg1, arg2, arg3) {
        alert('In override!');
        originalSomeFunction(arg1, arg2, arg3);
    };
})();

When I execute this code, I get "Can't execute code from a freed script".

当我执行此代码时,我收到“无法从释放的脚本执行代码”。

Thoughts? Is there a better way to do this? Testing in IE 6,7,8 and 9.

想法?有一个更好的方法吗?在 IE 6、7、8 和 9 中进行测试。

采纳答案by RMD

I figured out the solution.

我想出了解决办法。

Basically, you take all of the code I previously posted, and the execute it from within the context of the target frame using the eval() function. So...

基本上,您获取我之前发布的所有代码,并使用 eval() 函数在目标框架的上下文中执行它。所以...

myFrame.eval("SomeFunction = (function () {var originalSomeFunction = SomeFunction; return function (arg1, arg2, arg2) {alert('Inside override!'); originalSomeFunction(arg1, arg2, arg3);};})();");

Because the code is now within the target frame, it doesn't go out of scope, and we don't get the "freed" error anymore.

因为代码现在在目标框架内,它不会超出范围,我们不会再收到“释放”错误。

回答by Pointy

You cannot do that in IE, you've discovered. You need to make sure that any objects you pass between frames are native things like strings. Even "Date" instances have caused me problems, though that was on obscure versions of Windows 2000 back in the day.

你已经发现,你不能在 IE 中做到这一点。你需要确保你在帧之间传递的任何对象都是原生的东西,比如字符串。甚至“日期”实例也给我带来了问题,尽管当时是在 Windows 2000 的晦涩版本上。

By "freed script" what IE means is that your the page context where an object was "born" has been overwritten by a new page.

通过“释放的脚本”,IE 的意思是您“出生”对象的页面上下文已被新页面覆盖。

回答by Dbl

If you're comming here from google and are looking for an easy solution to fix this problem in IE:

如果您从 google 来到这里并正在寻找一个简单的解决方案来解决 IE 中的这个问题:

In our case the problem occured, because we were working with events inbetween iframes. By doing so it was possible that, upon changing the iframe contents combined with a triggered event it would attempt to call the script on the now changed iframe document. this raised the exception in question.

在我们的案例中,问题发生了,因为我们正在处理 iframe 之间的事件。通过这样做,在结合触发事件更改 iframe 内容时,它可能会尝试在现在更改的 iframe 文档上调用脚本。这引发了有问题的例外。

by adding

通过添加

$(window).on("unload", function(){
    $(target).off(eventname, handler);
});

the problem would cease to be raised. Finally no try/catch because of IE.

这个问题将不再被提出。由于 IE,最终没有 try/catch。

回答by Dead.Rabit

I know this question is quite old but in case you run into this I'd suggest you use JSON.parse to create an object on the parent frame rather than eval because eval is evil (causes security issues, I think it's disabled by default in some browsers too nowadays)

我知道这个问题很老但是如果你遇到这个我建议你使用 JSON.parse 在父框架上创建一个对象而不是 eval 因为 eval 是邪恶的(导致安全问题,我认为它在默认情况下被禁用现在也有一些浏览器)

for example, if you want to call someFunctionon frame 1, passing it a JSON object, use something like the below:

例如,如果您想someFunction在第 1 帧上调用,向其传递一个 JSON 对象,请使用如下所示的内容:

var frame = window.frames[1];
frame.someFunction( frame.JSON.parse( '{ "attr": 7 }' ) );