Skip to content Skip to sidebar Skip to footer

Can't Execute Code From A Freed Script

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: m

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.

Solution 4:

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 }' ) );

Post a Comment for "Can't Execute Code From A Freed Script"