2

好吧,我有一个包含框架集的页面。其中一个框架包含一个 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。

非常感谢!

4

1 回答 1

3

HTML5 不支持框架,如果您有正确的文档类型声明,您会看到一个空白页面。

无论如何,不​​需要使用 jQuery,这要简单得多:无论你想在哪里引用它们,你总是frameset引用你可以捕获的所有框架窗口(不需要)。当你需要一个文件时,它就在对象的正下方。在您的情况下:或.toptop.frame_nameidwindowtop.frame1.documenttop.document

另请注意,这foo是 的名称,而frameset不是任何框架。因此$('frame[name="foo"]', window.parent.document).attr("id", "foo");没有做你期望的事情。(或者也许只是帖子中的错字?)

这应该可以完成这项工作,尽管在in存在top之前可能已经准备好很长时间了。bodyframe2

$(document, top).ready(function () {    
    var testNode,
        frmObj = top.frame2,
        doc = frmObj.document;
    if (doc) {
        testNode = doc.createElement('div');
        testNode.id = 'testNode';
        doc.body.appendChild(testNode);
    }
});
于 2013-06-12T20:19:14.620 回答