我有一个带有iframe的页面。我放了一个代码,如果用户在x秒内不活动(没有按键和鼠标移动),页面将重定向到主页。我的问题是,代码只在主页中工作,但没有检测到iframe中的mousemove和keydown,因此尽管有活动,但每次在iframe中都会重定向。我尝试在iframe的onload事件中调用一个函数,但这不能正常工作。以下是该页面的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>SAMPLE</title>
<link href="main.css" rel="stylesheet" type="text/css" media="screen" />
<script language="javascript">
function redirect()
{
var timer;
window.onload= document.onmousemove= document.onkeypress= function(){
clearTimeout(timer);
timer=setTimeout(function(){location= 'index.html'},3000);
}
}
</script>
</head>
<body bgcolor="blue" onLoad="redirect();">
<iframe src="http://tplink.teleperformanceusa.com" frameBorder="0" width="100%" height="800px" onLoad="redirect();"></iframe>
...
</body>
</html>任何帮助都将不胜感激。谢谢!
发布于 2013-03-08 20:39:06
你的iframe设置为100%宽800px高。这几乎是你所有的网页。Iframes被设计为将其他网页的响应性部分插入到页面中。在您的代码中,整个页面看起来就是iframe。所以你必须问自己,如果iframe几乎是整个页面,为什么还需要iframe呢?为什么不直接链接到页面呢?
基本上,“主页”不会检测到iframe中的移动和活动,因为它超出了它的控制范围,因此将按照您编写的脚本执行操作。唯一发生的活动是在iframe中链接到的网页上。尽量避免使用iframe。
https://stackoverflow.com/questions/15285926
复制相似问题