【发布时间】:2016-04-06 10:46:56
【问题描述】:
首先我的项目有非常严格的限制:
- 离线可用性
- 尽可能简单的编码
=> PHP 和其他语言不是一个选项 - 只有 html 和 javascript。
结构:
- index.html
- frame.html
index.html 包含:
- 一个简单的 iframe:
- 用于禁用鼠标右键单击的 JavaScript:
- 另一个用于检测用户何时不活动的 JavaScript:
var timeoutID;函数设置(){
this.addEventListener("mousemove", resetTimer, false);
this.addEventListener("mousedown", resetTimer, false);
this.addEventListener("keypress", resetTimer, false);
this.addEventListener("DOMMouseScroll", resetTimer, false);
this.addEventListener("鼠标滚轮", resetTimer, false);
this.addEventListener("touchmove", resetTimer, false);
this.addEventListener("MSPointerMove", resetTimer, false);>
开始定时器(); } 设置();函数 startTimer() {
// 在调用 goInactive
之前等待 X 秒 timeoutID = window.setTimeout(goInactive, 3000); } 函数 resetTimer(e) {
window.clearTimeout(timeoutID);
去活动(); } 函数 goInactive() {
// 做点什么
警报(“超时”);
返回假; } 函数 goActive() {
// 做点什么
开始定时器(); }
- css 文件还包含一些对鼠标光标的限制:
身体{
背景颜色:#f0f0f0;
光标:无!重要;
-webkit 用户选择:无;
-moz 用户选择:无;
-ms 用户选择:无;用户选择:无;
}
=> 这对于 index.html 非常有效 - 但不适用于 iframe(目前是同一目录中的本地文件,但它也应该能够链接到网站上)。
我尝试了几种解决方法,但没有一种方法能够在 iframe 中保持网站活动(这样您也可以浏览 frame.html 的内容)。
我对任何解决方案持开放态度 - 如果它将脚本交给孩子,或者孩子从父母或任何覆盖解决方案那里获得脚本。 如果 iframe 在同一目录中的脱机文件上重定向有解决方法 - 这在第一时间会很好。
非常感谢您!
编辑:
这仍然有问题 - 它涉及到一个永无止境的循环:
$('iframe').load(function(){
$(this).contents().find("body").mousemove(function(){
var timeoutID;
function setup() {
this.addEventListener("mousemove", resetTimer, false);
this.addEventListener("mousedown", resetTimer, false);
this.addEventListener("keypress", resetTimer, false);
this.addEventListener("DOMMouseScroll", resetTimer, false);
this.addEventListener("mousewheel", resetTimer, false);
this.addEventListener("touchmove", resetTimer, false);
this.addEventListener("MSPointerMove", resetTimer, false);
startTimer();
}
setup();
function startTimer() {
timeoutID = (this).setTimeout(goInactive, 4000);
}
function resetTimer(e) {
(this).clearTimeout(timeoutID);
goActive();
}
function goInactive() {
alert("TIMEOUT");
return false;
goactive;
}
function goActive() {
startTimer();
}
});
});
$('iframe').attr("src","JavaScript:'iframe content'");
【问题讨论】:
-
您会始终控制 iframe 中的页面,还是希望嵌入任意页面?您可能应该将您感兴趣的事件侦听器放在框架中,但只有在您可以控制其内容的情况下才能这样做。
-
感谢您的回复 - 目前我可以完全控制 iframe 中的页面。但是为了将来的目的,一个外国网站上的链接也很好。目前超时仅与警报有关。但我希望网站在 XX 秒不活动后跳转到(父网站的!)起始页。如何通过实现 frame.html 中的脚本来实现?
-
另一种解决方案当然是消除 iframe - 但我不知道如何因为 PHP 不工作......?
标签: javascript html iframe parent offline