What causes the error "Unable to execute code from the freed script"

I thought I found a solution a while ago (see my blog ):

If you ever get a JavaScript error (or there must be JScript) "Unable to execute code from a freed script" - try moving any meta tags in your head so that they are in front of your script tags,

... but based on one of the latest blog comments, the fix I suggested may not work for everyone. I thought it would be nice to open up the StackOverflow community ....

What causes the "Cannot execute code from the freed script" error and what are the solutions / workarounds?

+45
javascript internet-explorer
Sep 17 '08 at 13:24
source share
12 answers

It looks like you have encountered an error / problem in how some tags are processed or you have links to released objects on which you are trying to execute methods.

First, I moved the <meta> tags before any <script> tags, as suggested here , and in many other places.

Then check if you have problems with the page / security here .

+19
Sep 17 '08 at 14:06
source share

This error occurs when calling a function that was created in a window or frame that no longer exists.

If you do not know in advance if the window still exists, you can try / catch to detect it:

 try { f(); } catch(e) { if (e.number == -2146823277) // f is no longer available ... } 
+31
Sep 17 '08 at 15:12
source share

The error occurs when the window of the parent script is located (i.e. closed), but refers to a script that is still held (for example, in another window). Although the “object” is still alive, the context in which it wants to execute is not.

It is somewhat dirty, but it works for my Windows sidebar gadget:

Here is a general idea: In the "main" window, a function is created that will define some code, yes, this is ugly. Then the “child” can call this “builder function” (which is bound to the main window area /) and returns a function that is also bound to the “main” window. The obvious drawback is, of course, that the function, the “bounce”, cannot be closed over the area in which it is apparently defined ... in any case, flexibility is enough:

This is partly pseudo-code, but I use it in the Windows sidebar gadget (I keep talking about this because the sidebar gadgets work in "unlimited zone 0", which may or may not change the script to a large extent.)

 // This has to be setup from the main window, not a child/etc! mainWindow.functionBuilder = function (func, args) { // trim the name, if any var funcStr = ("" + func).replace(/^function\s+[^\s(]+\s*\(/, "function (") try { var rebuilt eval("rebuilt = (" + funcStr + ")") return rebuilt(args) } catch (e) { alert("oops! " + e.message) } } // then in the child, as an example // as stated above, even though function (args) looks like it // a closure in the child scope, IT IS NOT. There you go :) var x = {blerg: 2} functionInMainWindowContenxt = mainWindow.functionBuilder(function (args) { // in here args is in the bound scope -- have at the child objects! :-/ function fn (blah) { return blah * args.blerg } return fn }, x) x.blerg = 7 functionInMainWindowContext(6) // -> 42 if I did my math right 

Alternatively, the main window should be able to pass the functionBuilder function to the child window - provided that the functionBuilder function is defined in the context of the main window!

It seems to me that I used too many words. YMMV.

+15
Sep 01 '09 at 5:35
source share

If you are trying to access a JS object, the easiest way is to create a copy:

 var objectCopy = JSON.parse(JSON.stringify(object)); 

Hope this helps.

+7
Feb 25 '14 at 16:32
source share

Here is a very specific case when I saw this behavior. It plays for me in IE6 and IE7.

From inside the iframe:

 window.parent.mySpecialHandler = function() { ...work... } 

Then, after reloading the iframe with the new content, in the window containing the iframe:

 window.mySpecialHandler(); 

This call fails with the error "Cannot execute code from the freed script" because mySpecialHandler was defined in a context (source DOM iframe file) that no longer exits. (An iframe reload destroyed that context.)

However, you can safely set the values ​​"serializable" (primitives, graphical objects that do not refer to functions directly) in the parent window. If you really need a separate window (in my case, an iframe) to indicate some work in the remote window, you can pass the work as String and "eval" in the receiver. Be careful with this, it usually does not for a clean or safe implementation.

+6
May 10, '10 at 22:50
source share

Starting with IE9, we started getting this error when calling .getTime () for a Date object stored in an array in another object. The solution was to make sure it was the date before calling the Date methods:

Error: rowTime = wl.rowData[a][12].getTime()

Pass: rowTime = new Date(wl.rowData[a][12]).getTime()

+5
Jul 12 '11 at 17:53
source share

This error can occur in MSIE when a child window tries to contact a parent window that is no longer open.

(Not quite the most useful error message text in the world.)

+3
Sep 17 '08 at 14:02
source share

I ran into this problem when inside a child frame I added a reference type to the top-level window and tried to access it after rebooting the child window

i.e.

 // set the value on first load window.top.timestamp = new Date(); // after frame reloads, try to access the value if(window.top.timestamp) // <--- Raises exception ... 

I managed to solve the problem using only primitive types

 // set the value on first load window.top.timestamp = Number(new Date()); 
+2
May 30 '13 at 19:19
source share

This is actually not an answer, but an example of how this happens.

We have frame A and frame B (it was not my idea, but I have to live with it). Frame A never changes; Frame B is constantly changing. We cannot apply code changes directly in frame A, therefore (according to the supplier’s instructions) we can only run JavaScript in frame B - an exact frame that continues to change.

We have a JavaScript fragment that needs to be run every 5 seconds, so the JavaScript in frame B creates a new script tag and inserts it into the main section of frame B. In this new script, there is setInterval (one entered), as well as a function to call. Despite the fact that embedded JavaScript is technically loaded by frame A (since it now contains a script tag), as soon as frame B changes, the function is no longer available to setInterval.

+1
Apr 29 '12 at 3:26
source share

I got this error in IE9 on a page that eventually opens an iFrame. Until iFrame was open, I could use localStorage. After the iFrame was opened and closed, I could no longer use localStorage due to this error. To fix this, I had to add this code to Javascript, which was inside the iFrame, as well as using localStorage.

 if (window.parent) { localStorage = window.parent.localStorage; } 
0
Apr 12 '13 at 13:54
source share

I received this error in DHTMLX when opening a dialog, and the parent identifier or current window identifier was not found.

  $(document).ready(function () { if (parent.dxWindowMngr == undefined) return; DhtmlxJS.GetCurrentWindow('wnManageConDlg').show(); }); 

Just make sure you send the correct curr / parent window id while opening the dialog

0
May 14 '15 at 5:39
source share

When updating iframe src I get this error.

Got this error by accessing the event (click in my case) of an element in the main window, like this (calling the main / external window directly):

 top.$("#settings").on("click",function(){ $("#settings_modal").modal("show"); }); 

I just changed it that way and it works fine (by invoking the parent of the parent of the iframe):

 $('#settings', window.parent.parent.document).on("click",function(){ $("#settings_modal").modal("show"); }); 

My iframe containing the modal is also inside another iframe.

0
Jun 13 '17 at 9:50
source share



All Articles