当它呈现给我时,这听起来几乎是不可能的事情。我知道您可以在离开网页时显示一个对话框进行确认。但是离开站点时是否可以显示一个对话框?
我无法找到/创建任何可以读取地址栏并知道您要离开网站的内容。
当它呈现给我时,这听起来几乎是不可能的事情。我知道您可以在离开网页时显示一个对话框进行确认。但是离开站点时是否可以显示一个对话框?
我无法找到/创建任何可以读取地址栏并知道您要离开网站的内容。
First off define which events can actually take your user away from your site?
So. what can you do about all these?
onclick
eventonsubmit
event of the form posting back outside of your site.onbeforeunload
event as much as you want, but you won't have much success of knowing what's going on. And there are certain limitations related to onbeforeunload
as well, so your hands will be tied most of the time.Why would you want to control this event anyway except for bothering your users not to leave you. Begging doesn't give much justice in the web world anyway. And when some site would bother me with messages or even worse prevent me from leaving I wouldn't want to get back anymore. It smells of bad bad bad usability and gives a hint of adware site.
Rather try to keep your users interested by providing them with valuable content.
您最好的选择是收听非标准beforeunload
事件。几乎所有浏览器都支持这一点,除了众所周知的 Opera,它非常严格地遵守 W3C 标准。
开球示例:
window.onbeforeunload = function() {
return "You're leaving the site.";
};
此消息将以确认对话框的形式显示。
null
在您的特定情况下,每当单击导航链接或提交内部表单时,您都需要将其关闭(只需设置为)。您可以通过侦听click
所需链接的submit
事件和所需表单的事件来做到这一点。jQuery在这里可能会有很大帮助:
window.onbeforeunload = function() {
return "You're leaving the site.";
};
$(document).ready(function() {
$('a[rel!=ext]').click(function() { window.onbeforeunload = null; });
$('form').submit(function() { window.onbeforeunload = null; });
});
您只需要为所有外部链接赋予事实上的标准属性rel="ext"
来表示它们是外部链接。
<a href="http://google.com" rel="ext">Google</a>
This may help
onclick
event before attach initLocalLinkException()
;HTML:
<a href="test.html">internal link</a>
<a href="#test">html anchor</a>
<a href="http://www.google.com">external link</a>
<a href="http://www.google.com" target="_blank">blank external link</a>
<form action="test.html" method="post" >
<button type="submit">Post Button</button>
</form>
JavaScript:
$(document).ready(function () {
initLocalLinkException();
window.onbeforeunload = function () { confirmExit() };
$('form').submit(function () {
window.onbeforeunload = null;
});
});
function initLocalLinkException() {
$('a').click(function () {
if ($(this).attr('target') != '_blank') {
var link = $(this).attr('href');
if (link.substr(0, 4) == 'http') {
var LocalDomains = new Array('http://www.yourdomain.com',
'https://yourdomain.com',
'localhost', '127.0.0.1');
var matchCount = 0;
$.each(LocalDomains, function () {
if (this == link.substr(0, this.length)) {
matchCount++;
}
});
if (matchCount == '0') {
confirmExit();
} else {
window.onbeforeunload = null;
}
} else { window.onbeforeunload = null; }
}
});
}
function confirmExit() {
alert('Are you sure?'); // Do whatever u want.
}
看看这个线程。
One possible way to achieve this would be to use Javascript to examine all of the a
tags on your page when it loads and check if they are linking to an external site. If so, you can add an onclick
event to show a confirm/alert box or something more elegant. Of course, using jQuery will greatly simplify the Javascript you'll have to write, like in the above thread.
Using the expression from this question, you can do the following:
$.expr[':'].external = function(obj){
return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname);
};
$.expr[':'].internal = function(obj){
return obj.hostname == location.hostname;
};
$(function() {
var unloadMessage = function() {
return "Don't leave me!";
};
$('a:internal').click(function() {
window.onbeforeunload = null;
});
$('form').submit(function() {
window.onbeforeunload = null;
});
$('a:external').click(function() {
window.onbeforeunload = unloadMessage;
});
window.onbeforeunload = unloadMessage;
});
It's possible. Just try entering a question or answer to SO and then navigating away before submitting it. It doesn't matter whether you click on a link or type in the address bar, you get an "Are you sure?" alert. You might post over on SO Meta asking how they do this.
You can do that if you design your site as a one page web app.
It means, a single page is loaded, then other contents are loaded dynamically using ajax.
In that case the onbeforeunload
is triggered when the user leave the page/site.