3

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.

RMD
  • 3,421
  • 7
  • 39
  • 85
  • works fine for me in FF, Chrome and IE.. what browser are you running this ? It might be some other part in your code that is causing this.. – Gabriele Petrioli Dec 06 '11 at 23:28
  • 1
    The frame most likely no longer exists at the time you try to access it. See: http://stackoverflow.com/questions/83132/what-causes-the-error-cant-execute-code-from-a-freed-script – Wayne Dec 06 '11 at 23:29
  • I **need** to know what does a `freed script` means! – Pierre Dec 06 '11 at 23:48
  • 3
    Guys, "freed script" is part of the error message I'm getting from IE. If you don't know what "freed script" means, don't respond to my question. – RMD Dec 06 '11 at 23:53
  • Just to clarify, "myFrame" is not being reloaded during this execution. – RMD Dec 06 '11 at 23:54
  • 1
    Freed in this context means freed from the memory, so deleted/missing. I found out from @iwburk's comment – Dead.Rabit May 13 '13 at 13:50

4 Answers4

4

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • So there is no way to override a function in a different frame in IE? What's strange is that even if I use variables in "myFrame" to hold the function pointers, I still get this error. – RMD Dec 06 '11 at 23:51
  • Yes, you can, but the problem happens when you have a "stale" object that was born in another page. As soon as your code tries to touch it, IE realizes that its prototype object (and etc.) are gone. It's just the way that the JScript mechanism seems to work. – Pointy Dec 06 '11 at 23:56
  • What in my code represents the "stale" object? How can I modify this code to prevent this from happening? – RMD Dec 07 '11 at 00:00
  • Well it probably has something to do with things passed back and forth *between* the pages. Or, perhaps, if you override the function in the other frame and then the frame from which you do that is overwritten. Now, if your frames are completely static and they're never reloaded with new pages from the server, then that's a new one on me. – Pointy Dec 07 '11 at 00:03
3

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.

Dbl
  • 5,634
  • 3
  • 41
  • 66
  • `handler` is optional - so you can do (for example) `.off('click')` if you can clear all jQuery click handlers on the target element. – Stan Jul 23 '15 at 20:01
2

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.

RMD
  • 3,421
  • 7
  • 39
  • 85
0

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 }' ) );
Dead.Rabit
  • 1,965
  • 1
  • 28
  • 46