好吧,我有一个包含框架集的页面。其中一个框架包含一个 asp 页面,该页面包含一个用 javascript 编写的脚本。此脚本需要能够访问包含在另一个框架(在框架集中)中的 asp 页面的内容。这是框架集代码的示例:
<frameset name="foo">
<frame name="frame1" src="mydomain/path/of/the/page.asp">
<frame name="frame2" src="mydomain/path/of/the/other/page.asp">
因此,基本上包含在 frame1 中的页面内的脚本会尝试访问包含在 frame2 中的页面内的内容(更具体地说是尝试插入 DOM 元素)。
两个框架都在同一个域(和子域)上,所以跨脚本应该不是问题。我无法在脚本之外编辑代码,所以我不得不使用框架。这是我当前的代码:
//Ideally, I would like to make sure the page contained within frame2 is loaded before trying to access it's elements. I still need to figure that one out.
$(document, window.parent).ready( function () {
//Assign id "foo" to frame2 using jQuery
$('frame[name="foo"]', window.parent.document).attr("id", "foo");
var doc;
var testNode;
var frmObj = window.parent.document.getElementById('foo');
if (frmObj.contentDocument) { // DOM
doc = frmObj.contentDocument;
}
else if (frmObj.contentWindow) { // IE
doc = frmObj.contentWindow.document;
}
if (doc) {
testNode = doc.createElement('div');
testNode.Id = "testNode";
doc.getElementByTagName('body').appendChild(testNode);
}
});
包含 getElementByTagName 的行在 IE 10 中引发错误:
SCRIPT438:对象不支持属性或方法“getElementByTagName”
似乎文档对象没有任何名为 getElementByTagName 的方法。在这种情况下,可以说文档对象不存在,这是荒谬的,因为我在尝试调用它的方法之前已经测试了它的存在。知道了这一切,我们该如何解决这个问题?
备注:
- 我无法使用其他浏览器测试该问题,因为所有页面所需的外部代码都使用 vbscript。
- id "foo" 已成功添加到 frame2。
非常感谢!