有以下两个网页:
test1.htm:<html>
<head>
<title>test1.htm</title>
</head>
<body>
<input type="button" value="Open New Window" onclick="javascript:window.open('test2.htm');">
</body>
</html>
test2.htm:<html>
<head>
<title>test2.htm</title>
</head>
<body>
<textarea id='t' cols='150' rows='30'></textarea>
<input type='button' value='执行' onclick="javascript:eval(document.getElementById('t').value)">
</body>
</html>先打开test1.htm,点击button打开test2.htm,在TEXTAREA中输入var i=1;
var tmid=window.setInterval("te()",200);
alert(tmid);
function te()
{
if (i<4){alert(i);}else{window.clearInterval(tmid);}
}点击“执行”按钮,网页并不执行te(),总报"行1缺少对象",那位大侠看看错在哪里???谢谢~~~~
test1.htm:<html>
<head>
<title>test1.htm</title>
</head>
<body>
<input type="button" value="Open New Window" onclick="javascript:window.open('test2.htm');">
</body>
</html>
test2.htm:<html>
<head>
<title>test2.htm</title>
</head>
<body>
<textarea id='t' cols='150' rows='30'></textarea>
<input type='button' value='执行' onclick="javascript:eval(document.getElementById('t').value)">
</body>
</html>先打开test1.htm,点击button打开test2.htm,在TEXTAREA中输入var i=1;
var tmid=window.setInterval("te()",200);
alert(tmid);
function te()
{
if (i<4){alert(i);}else{window.clearInterval(tmid);}
}点击“执行”按钮,网页并不执行te(),总报"行1缺少对象",那位大侠看看错在哪里???谢谢~~~~
你是不是少了<script type="text/javascript"></script>
var i=1;
function te()
{
if (i<4){alert(i);}else{window.clearInterval(tmid);}
}
var tmid=window.setInterval("te()",200);先定义,后调用
window.setInterval(te,200);
function te()
{
if (i<4){alert(i);i++}else{window.clearInterval(tmid);}
}
经测试是可以的。感觉使用eval不会存在先定义后调用的限制吧,毕竟是一个域。还有调用函数还是习惯只写函数名。
function a() {
eval("function f(){ };");
}
a();
alert(typeof f); // undefined只有在同一命名空间里声明才可以访问
eval("function e(){ };");
alert(typeof e); // function解决方案:指定声明为window的方法
<textarea id='t' cols='150' rows='30'>
var i=1;
var tmid=window.setInterval("te()",200);
alert(tmid);
window.te = function() {
if (i<4){alert(i);}
else{window.clearInterval(tmid);}
}
</textarea>