Can't execute code from a freed script

前端 未结 4 647
挽巷
挽巷 2021-01-21 05:51

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 followin

相关标签:
4条回答
  • 2021-01-21 06:20

    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...

    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.

    0 讨论(0)
  • 2021-01-21 06:26

    If you're comming here from google and are looking for an easy solution to fix this problem in 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.

    by adding

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

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

    0 讨论(0)
  • 2021-01-21 06:40

    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)

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

    var frame = window.frames[1];
    frame.someFunction( frame.JSON.parse( '{ "attr": 7 }' ) );
    
    0 讨论(0)
  • 2021-01-21 06:43

    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.

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

    0 讨论(0)
提交回复
热议问题