外部链接所需的OnUnload消息 [英] OnUnload message needed for external links

查看:74
本文介绍了外部链接所需的OnUnload消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个仅当人们离开当前网页而不是当前网站时才会显示的消息脚本.

I need a message script that will only come up when people are leaving the current webpage and not the current website.

当人们完全离开网站时,会出现该消息,他们需要按OK(确定)按钮以停留在当前页面(并取消以退出网站).

When people are leaving the website entirely, the message will come up and they will need to press the OK button to stay at the current page (and cancel to leave the website).

当人们实际停留在网站上或单击内部链接或页面时,该脚本可能无法运行.

The script may not run when people actually stay on the website or when they click on internal links or pages.

可以做到吗?

推荐答案

签出这个非常基本的示例解决方案.它设置onbeforeunload处理程序,但是如果用户单击内部链接,则将其删除.下面是示例解决方案中代码的通用版本.

Check out this very basic example solution. It sets the onbeforeunload handler but then removes it if the user clicks an internal link. Below is a generic version of the code from the example solution.

HTML :

<a href="internal_link.html">internal link</a>
<a href="http://www.example.com">external link</a>

JS (使用 jQuery ):

window.onbeforeunload = function(){
    return "Are you sure you want to leave our website?";
}
$(function(){
    $('a, input, button').click(function(){
            window.onbeforeunload = null;
    });
});

这篇关于外部链接所需的OnUnload消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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