本文介绍了在Chrome上的beforeunload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,可以在用户关闭页面时将其重定向到另一个URL.这适用于Firefox,IE,Safari等,但不适用于Chrome.它不会将用户重定向到新的URL.

I have this code that will redirect the user to another URL whenever he/she closes the page. This is working on Firefox, IE, Safari, etc. but not in Chrome. It won't redirect the user to new URL.

谢谢!

jQuery(document).ready(function() { 
    jQuery(window).bind('beforeunload', function(e) {  
        location.href="http://google.com/";
        return "Before you leave, please take a look at this limited time offer.";
    });
});

推荐答案

不确定这是您要寻找的内容,如果用户选择保留在页面上,则会重定向用户:

Not sure it is what you are looking for, this will redirect user if he choices to stay on page:

DEMO

var a,b;
window.onbeforeunload = function (e) {
    if (b) return;
    a = setTimeout(function () {
        b = true;
        window.location.href = "http://google.com";
    }, 500);
    return "Before you leave, please take a look at this limited time offer.";
}
window.onunload = function () {
    clearTimeout(a);
}

这篇关于在Chrome上的beforeunload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:31