请在 Chrome 浏览器中检查我的代码,如果您点击刷新,系统会提示您两个选项。
- Leave This Page and
- Stay on This Page
当我点击 2.留在这个页面按钮,它必须激活我的自定义函数 displayMsg()
谁能给我解决方案?
<script type="text/javascript">
function displayMsg() {
alert('my text..');
}
</script>
<script type="text/javascript">
window.onbeforeunload = function(evt) {
var message = 'Please Stay on this page and we will show you a secret text.';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
return message;
}
trace(evt);
}
</script>
最佳答案
使用计时器来监听变量的变化:
var vals=0;
function displayMsg() {
alert('my text..');
}
window.onbeforeunload = function evens(evt) {
var message = 'Please Stay on this page and we will show you a secret text.';
if (typeof evt == 'undefined') {
evt = window.event;
}
timedCount();
vals++;
if (evt) {
evt.returnValue = message ;
return message ;
}
trace(evt);
}
function timedCount()
{
t=setTimeout("timedCount()",100);
if(vals>0)
{
displayMsg();
clearTimeout(t);
}
}
关于Javascript 当我们在 Chrome 中选择“停留在页面上”时如何调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10616437/