Javascript中,I&QUOT能;重定向"用户onbeforeunload?如果不能,如何 [英] Javascript, Can I "redirect" user in onbeforeunload? If cant, how to

查看:86
本文介绍了Javascript中,I&QUOT能;重定向"用户onbeforeunload?如果不能,如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能当用户关闭浏览器重定向到另一个页面?

Is it possible to redirect to another page when the user closes the browser?

尝试:


  1. 我试过onunload的,不能正常工作

  1. I tried onunload, does not work

window.onunload = function redirect(){...}


  • 我也试过另一种方法,它不工作之一:

  • I also tried another method, it doesn't work either:

    window.onbeforeunload = redirect(){...}
    


  • <身体onbeforeunload =返回false; redirecty()>

    第三的方法,我想取消 onbeforeunload (指延时关闭浏览器)时,我称之为重定向功能, window.confirm ,如果是重定向,如果没有然后关闭浏览器。不过,这并不正常工作。

    The 3rd method, I want to cancel the onbeforeunload (means delay closing the browser), the I call the redirect function, window.confirm, if yes redirect, if no then close the browser. But it does not work as well.

    有没有其他办法?也许提示,让用户选择是否重定向到新的页面时,他/她关闭浏览器?我跑出来的想法......

    Is there any other way?? Maybe prompt to let user select whether to redirect to new page when he/she close the browser? I'm running out of ideas...

    推荐答案

    您onbeforeunload事件需要返回一个字符串(不等同于假),浏览器将包含此字符串显示给用户自己的消息。

    Your onbeforeunload event needs to return a string (that doesn't equate to false), the browser will include this string in its own message displayed to the user.

    window.onbeforeunload = function(){
        	location.assign('http://www.google.com');
        	return "go to google instead?";
        }
    

    然而,这将是真正棘手的话你的信息的方式,用户将能够明白该怎么做。而且我不知道这是在每个浏览器功能强大,我只是尝试在浏览器,它的工作,但我结束了一个标签我不能收!我设法通过幸好Chrome的任务管理器将其杀死。

    However, it's going to be really tricky to word your message in a way that the user would be able to understand what to do. And I'm not sure this is robust in every browser, I just tried it in Chrome, it worked, but I ended up with a tab I could not close! I managed to kill it via the Chrome task manager thankfully.

    这篇关于Javascript中,I&QUOT能;重定向"用户onbeforeunload?如果不能,如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆