为什么获取不到js全局变量--高分跪求大神帮我解决

qianfoyuan 2011-09-30 10:13:02
我要获取的确是 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 全局变量?

...全文
469 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
峭沙 2011-09-30
  • 打赏
  • 举报
回复
呵呵,document.write要慎用
qianfoyuan 2011-09-30
  • 打赏
  • 举报
回复
非常感谢 axiheyhey 谢谢你了.
qianfoyuan 2011-09-30
  • 打赏
  • 举报
回复
好谢谢三楼,非常谢三楼,我明白了,把
var str="<div><iframe width=400 height=300 frameborder='yes' scrolling='no' style='position:absolute'></iframe></div>";
document.write(str);
放在window.onload外可以避免函数调用导致window销毁.同时也感谢二楼
峭沙 2011-09-30
  • 打赏
  • 举报
回复
因为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>
晨晨 2011-09-30
  • 打赏
  • 举报
回复
你在JS文件中,不要将变量放在<script language=javascript></script>之间,即不需要这个标签,就可以了
Crazywa 2011-09-30
  • 打赏
  • 举报
回复
亲,你defer了...

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧