调用Iframe js Class

时间:2010-11-17 19:05:23

标签: javascript iframe mootools

我处于这种情况

index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" src="mootools.js"></script>
<script>
window.addEvent('domready', function() {

 var b=document.createElement('iframe');
 b.src="about:blank";
 b.style.height="0px";
 b.style.width = "0px";
 b.style.visibility="hidden";
 b.style.border="none";
 b.id = "my_frame";
 b.src="about:blank";
        var headID = document.getElementsByTagName("head")[0];
 headID.appendChild(b);
 var val = '<html><head>';
        val += '<scr' + 'ipt type="text/javascript" src="test.class.js"></scr' + 'ipt>';

        val += '</head><body></body></html>';
        var doc = b.contentWindow || b.contentDocument;

        if (doc.document){
            doc = doc.document
        }

        doc.open();
        doc.write(val);
        doc.close();
});
</script>
</head>
<body>
<div id="main"></div>
<script>var a = new Test();</script>
<script>a.doSomething();</script>
</body>
</html>

test.class.js:

var Test = new Class({

doSomething: function(){

alert("ok!");

}

});

是否可以将Test类注入父窗口,而不使用parent.frames["my_frame"].func();之类的东西?我只需要用这种方式来调用class:

<script>var a = new Test();</script>
<script>a.doSomething();</script>

1 个答案:

答案 0 :(得分:1)

不应该这样的工作吗?

var a = new parent.frames["my_frame"].Test();
a.doSomething();