我要获取的确是 A.js 中全局变量 b,各位可以将下面代码拷下去,分成两个文件, A.js test.html.然后点击 test.html里面会生成一个 iframe 框架,但我要获取 父窗体中 b 变量,但 IE Firefox 提示 b 未定义.跪求大神帮我解决,解决问题者给高分.万分感谢.JS A页面:
function A()
{
this.x = 6;
this.y = 7;
}
var b = 5;
HTML 页面:
<html>
<head>
<script type="text/javascript" src="./A.js"></script>
<script type="text/javascript">
var x = 5;
var a = new A();
</script>
</head>
<body>
<input id = "readme" type = "text" value="helloworld"></input><br>
<script>
window.onload=function(){
var str="<div><iframe width=400 height=300 frameborder='yes' scrolling='no' style='position:absolute'></iframe></div>";
document.write(str); var mystr = "";
mystr += "<html>";
mystr += " <head>";
mystr += " <script type='text/javascr" + "ipt' defer>";
mystr += " function func(){";
mystr += " var x=parent.b;document.getElementsByTagName('input')[0].value=x;"; mystr += " }";
mystr += " </" + "scr" + "ipt>";
mystr += " </head>";
mystr += " <body>";
mystr += " <input type='button' value='' onclick='func();'></input>";
mystr   += " </body>";
mystr += "</head>"; window.frames[0].document.write(mystr);
window.frames[0].document.close();}
</script>
<p>一些老的浏览器不支持 iframe。</p>
<p>如果得不到支持,iframe 是不可见的。</p>
</body>
</html>为什么不能获取不到 b 全局变量?

解决方案 »

  1.   

    你在JS文件中,不要将变量放在<script language=javascript></script>之间,即不需要这个标签,就可以了
      

  2.   

    因为document.write(str);这句话,你把父窗口的所有元素,包括js全部清除掉了。。
    <html>
    <head>
    <script type="text/javascript">
    function A(){
    this.x = 6;
    this.y = 7;
    alert(this.x);
    }
    c = 1;
    var b = 5;
    var x = 5;
    //var a = new A();
    </script>
    </head>
    <body>
    <input id = "readme" type = "text" value="helloworld"></input><br>
    <script>
    window.onload=function(){
    //var str="<iframe width=400 height=300 frameborder='yes' scrolling='no' style='position:absolute'></iframe>";
    //document.write(str);
    var iframe = document.createElement('iframe');
    iframe.width = 400;
    iframe.height = 300;
    iframe.frameborder = 'yes';
    iframe.scrolling='no';
    iframe.style.position = 'absolute';
    document.body.appendChild(iframe);


    var mystr = "";
    mystr += "<html>";
    mystr += " <head>";
    mystr += "  <script type='text/javascr" + "ipt'>";
    mystr += "  function func(){";
    mystr += "  var x=parent.b;document.getElementsByTagName('input')[0].value=x;";
    mystr += "  }";
    mystr += "  </" + "scr" + "ipt>";
    mystr += " </head>";
    mystr += " <body>";
    mystr += "  <input type='button' value='' onclick='func();'></input>";
    mystr += " </body>";
    mystr += "</head></html>";
    window.frames[0].document.open();
    window.frames[0].document.write(mystr);
    window.frames[0].document.close();
    }
    </script>
    <p>一些老的浏览器不支持 iframe。</p>
    <p>如果得不到支持,iframe 是不可见的。</p>
    </body>
    </html>
      

  3.   

    好谢谢三楼,非常谢三楼,我明白了,把
    var str="<div><iframe width=400 height=300 frameborder='yes' scrolling='no' style='position:absolute'></iframe></div>";
    document.write(str);
    放在window.onload外可以避免函数调用导致window销毁.同时也感谢二楼
      

  4.   

    非常感谢 axiheyhey 谢谢你了.