开发者

How to access the window object from the document object

开发者 https://www.devze.com 2023-04-09 07:37 出处:网络
Is it possible to access the window object directly from the document object in Javascript? For example:

Is it possible to access the window object directly from the document object in Javascript?

For example:

// window.frames[0] returns the document object of the first frame or iframe found on the page
myFunc(window.frames[0]);

function myFunc(doc) {
  // I want to do something along these lines:
  var wnd = doc.getWindow();
  alert("Found frame: " + wnd.name);
  for (var i=0; i&l开发者_开发百科t;wnd.frames.length; i++) {
    myFunc(wnd.frames[i]);
  }
}

I can't use jQuery for this, sorry.


According to the MDN documentation you should already be getting the window with window.frames[0]. If you want the actual document you need to grab the actual frame element and dig into the document.

var firstFrame = document.getElementsByTagName( "iframe" )[ 0 ];
firstFrame.contentWindow;  // The window
firstFrame.contentWindow.document;  // The document

Note: I believe contentWindow isn't supported in very early versions of Safari (pre-3.0 IIRC)

0

精彩评论

暂无评论...
验证码 换一张
取 消