admin管理员组

文章数量:1391987

How can I make a new Window object, myWindow, that is independent to window (so modifying e.g. myWindow.Array.prototype does not effect window.Array.prototype), without creating an <iframe>?

At the moment I'm doing it as follows

function newWindow(){
    var myFrame = document.createElement('iframe'), myWindow = undefined;
    myFrame.style.display = 'none';
    myFrame.src = 'javascript:undefined;';
    document.body.appendChild(myFrame);
    myWindow = myFrame.contentWindow;
    document.body.removeChild(myFrame);
    return myWindow;
}

Ultimately, I'd like to make my own copies of core object types and prototype them.

How can I make a new Window object, myWindow, that is independent to window (so modifying e.g. myWindow.Array.prototype does not effect window.Array.prototype), without creating an <iframe>?

At the moment I'm doing it as follows

function newWindow(){
    var myFrame = document.createElement('iframe'), myWindow = undefined;
    myFrame.style.display = 'none';
    myFrame.src = 'javascript:undefined;';
    document.body.appendChild(myFrame);
    myWindow = myFrame.contentWindow;
    document.body.removeChild(myFrame);
    return myWindow;
}

Ultimately, I'd like to make my own copies of core object types and prototype them.

Share Improve this question asked Sep 14, 2012 at 0:09 Paul S.Paul S. 66.4k9 gold badges128 silver badges143 bronze badges 1
  • Related article: How ECMAScript 5 still does not allow to subclass array – Oriol Commented May 4, 2014 at 17:23
Add a ment  | 

2 Answers 2

Reset to default 3

Emm... you can't do that. Well... you may call window.open, but it will open a new window. And... why do you need that at all? It looks like you are on wrong way.

Depending on what you are doing, a better approach may be to create a normal array, then augment that one particular array instance with the features you need.

function augmentArray(a) {
  a.purgeAll = function() {a.length = 0;};
}

var myList = [1, 2, 3];
augmentArray(myList);
myList.purgeAll();

本文标签: javascriptHow can I create a new window object without iframesStack Overflow