Take the 2-minute tour ×

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

If you ever get the JavaScript (or should that be JScript) error "Can't execute code from a freed script" - try moving any meta tags in the head so that they're before your script tags.

...but based on one of the most recent blog comments, the fix I suggested may not work for everyone. I thought this would be a good one to open up to the StackOverflow community....

What causes the error "Can't execute code from a freed script" and what are the solutions/workarounds?

share|improve this question
add comment

10 Answers

up vote 8 down vote accepted

It sounds like you've hit a bug/problem in the way some tags are handled or that you have references to released objects on which you are trying to execute methods.

First I'd move any <meta> tags before any <script> tags as suggested here and here and numerous other places.

Then check to see if you have page/security issues discussed here.

share|improve this answer
Psst.. One of your links is for the OP's blog.... –  erlando Sep 17 '08 at 14:13
Moved <meta> tags before any <script> tags solved the issue for me too, with IE6 –  Enrico Detoma Nov 25 '09 at 10:18
i just got the error without any meta tags on my page –  Daniel Brink Sep 27 '12 at 11:22
link rot. Please fix or remove the links. –  Benjamin Gruenbaum Jan 15 at 9:27
add comment

You get this error when you call a function that was created in a window or frame that no longer exists.

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

  if (e.number == -2146823277)
    // f is no longer available
share|improve this answer
would this do the same thing? (typeof f == "undefined") { /* not avail */ } –  TJR Nov 4 '11 at 16:06
I'm not sure, but I wouldn't count on it. I'm guessing that typeof f would throw an error, or otherwise returns "function" or "unknown". –  Sjoerd Visscher Nov 5 '11 at 12:53
Thanks Sjoerd..But why would this just happen in IE? –  ManJan Mar 26 '13 at 16:03
It depends on the implementation of the browser. I think in IE each window has its own Javascript engine. In newer browsers (perhaps also in newer IE versions) there's a shared engine exactly when two windows have access to each others code, because one window was opened by the other. –  Sjoerd Visscher Mar 26 '13 at 21:12
add comment

The error is caused when the 'parent' window of script is disposed (ie: closed) but a reference to the script which is still held (such as in another window) is invoked. Even though the 'object' is still alive, the context in which it wants to execute is not.

It's somewhat dirty, but it works for my Windows Sidebar Gadget:

Here is the general idea: The 'main' window sets up a function which will eval'uate some code, yup, it's that ugly. Then a 'child' can call this "builder function" (which is /bound to the scope of the main window/) and get back a function which is also bound to the 'main' window. An obvious disadvantage is, of course, that the function being 'rebound' can't closure over the scope it is seemingly defined in... anyway, enough of the gibbering:

This is partially pseudo-code, but I use a variant of it on a Windows Sidebar Gadget (I keep saying this because Sidebar Gadgets run in "unrestricted zone 0", which may -- or may not -- change the scenario greatly.)

// 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's 
// 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

As a variant, the main window should be able to pass the functionBuilder function to the child window -- as long as the functionBuilder function is defined in the main window context!

I feel like I used too many words. YMMV.

share|improve this answer
i got the error just now. i'm doing something similar to the above code - iframe calling js in the parent window's scope. hahaha fun :) –  Daniel Brink Sep 27 '12 at 11:26
add comment

Here's a very specific case in which I've seen this behavior. It is reproducible for me in IE6 and IE7.

From within an iframe:

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

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


This call fails with "Can't execute code from a freed script" because mySpecialHandler was defined in a context (the iframe's original DOM) that no longer exits. (Reloading the iframe destroyed this context.)

You can however safely set "serializeable" values (primitives, object graphs that don't reference functions directly) in the parent window. If you really need a separate window (in my case, an iframe) to specify some work to a remote window, you can pass the work as a String and "eval" it in the receiver. Be careful with this, it generally doesn't make for a clean or secure implementation.

share|improve this answer
add comment

Beginning in IE9 we began receiving this error when calling .getTime() on a Date object stored in an Array within another Object. The solution was to make sure it was a Date before calling Date methods:

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

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

share|improve this answer
yes. IE9 will mangle types. You may also get it with arrays. You can do Array.prototype.slice.call(data) if you want. Sometimes it will throw this error, sometimes it will throw E_UKNOWN ... it's usually caused by a spurious "delete" improperly invoked and unfortunately has nothing to do with the child/parent issue; it's the browser going nutso over something and giving you a red herring. –  user535759 Aug 3 '11 at 4:53
add comment

This error can occur in MSIE when a child window tries to communicate with a parent window which is no longer open.

(Not exactly the most helpful error message text in the world.)

share|improve this answer
Or vice versa (parent to child) –  erlando Sep 17 '08 at 14:15
add comment

This isn't really an answer, but more an example of where this precisely happens.

We have frame A and frame B (this wasn't my idea, but I have to live with it). Frame A never changes, Frame B changes constantly. We cannot apply code changes directly into frame A, so (per the vendor's instructions) we can only run JavaScript in frame B - the exact frame that keeps changing.

We have a piece of JavaScript that needs to run every 5 seconds, so the JavaScript in frame B create a new script tag and inserts into into the head section of frame B. The setInterval exists in this new scripts (the one injected), as well as the function to invoke. Even though the injected JavaScript is technically loaded by frame A (since it now contains the script tag), once frame B changes, the function is no longer accessible by the setInterval.

share|improve this answer
add comment

I got this error in IE9 within a page that eventually opens an iFrame. As long as the iFrame wasn't open, I could use localStorage. Once the iFrame was opened and closed, I wasn't able to use the localStorage anymore because of this error. To fix it, I had to add this code to in the Javascript that was inside the iFrame and also using the localStorage.

if (window.parent) {
    localStorage = window.parent.localStorage;
share|improve this answer
add comment

I ran into this problem when inside of a child frame I added a reference type to the top level window and attempted to access it after the child window reloaded


// 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 was able to resolve the issue by using only primitive types

// set the value on first load
window.top.timestamp = Number(new Date());
share|improve this answer
add comment

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

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

Hope it'll help.

share|improve this answer
add comment

Your Answer


