admin管理员组文章数量:1026989
I'm looking for a way to display a message to the user if he leaves my site after only viewing one page.
I found this () clever solution, but it has a few flaws:
staying_in_site = false;
Event.observe(document.body, 'click', function(event) {
if (Event.element(event).tagName == 'A') {
staying_in_site = true;
}
});
window.onunload = popup;
function popup() {
if(staying_in_site) {
return;
}
alert('I see you are leaving the site');
}
It displays the message also when refreshing the page or using the back button.
Do you know a better solution or how to fix it in the above code? I'm no javascript master :)
My intention is to add the code on very specific landing pages only, and display the message when people leave the page without downloading my trial software or reading other pages on my site.
I'm looking for a way to display a message to the user if he leaves my site after only viewing one page.
I found this (http://www.pgrs/2008/1/30/popup-when-leaving-website) clever solution, but it has a few flaws:
staying_in_site = false;
Event.observe(document.body, 'click', function(event) {
if (Event.element(event).tagName == 'A') {
staying_in_site = true;
}
});
window.onunload = popup;
function popup() {
if(staying_in_site) {
return;
}
alert('I see you are leaving the site');
}
It displays the message also when refreshing the page or using the back button.
Do you know a better solution or how to fix it in the above code? I'm no javascript master :)
My intention is to add the code on very specific landing pages only, and display the message when people leave the page without downloading my trial software or reading other pages on my site.
Share Improve this question edited May 2, 2010 at 14:57 skaffman 404k96 gold badges824 silver badges775 bronze badges asked May 2, 2010 at 14:55 JubbaJubbaJubbaJubba 3772 gold badges6 silver badges14 bronze badges 4- 10 I do not want to help you do this!!! – Galen Commented May 2, 2010 at 14:59
- 7 Please don't do this, so annoying! – Oskar Kjellin Commented May 2, 2010 at 15:02
- display the message when people leave the page without downloading my trial software or reading other pages on my site Yeah, most (smart) users flag those sites as "dangerous" on WOT. – nc3b Commented May 2, 2010 at 15:03
- @Brian, consider what is going on in the mind of a user when they decide to leave your website without downloading your trial software. They have already decided your software isn't for them at that particular time, and now you're going to throw up a dialog asking them to reconsider? Do you realize how insanely rude and infuriating that type of message is? Trust me - if people aren't downloading your software now, they sure as hell won't download it when you implement something like this. Be nice to your users. Don't be a jerk. – Rob Commented May 2, 2010 at 15:47
3 Answers
Reset to default 6I will start by saying that I will not, in any way, remend that you do this. It's a bad practice, it only annoys users and it makes you look extremely desperate. I repeat, don't do this, it's not a good idea, it's silly and it's not a good idea.
But the way you can do it is to only display the message when users are clicking on links that lead away from the site.
You can do this by looking at the href
attribute of the link and check if it's an external site. If it is, then display the message
But I repeat, don't do this!
We might be able to forgive you for window.onbeforeunload
. But that's it!
There are some cases where this can be very useful and not having this feature will be annoying. One of the applications I developed for a client required users to input a lot of data on a page (close to 25 fields). I know it would have been better and more usable if the form was broken into more pages, but that's not the point here. There were instances where employees (mostly not very savvy puter users) entered data but for whatever reason left the page without submitting (accidentally, I am sure), resulting in frustration. I was happy to do window.onbeforeunload
in that case.
I'm looking for a way to display a message to the user if he leaves my site after only viewing one page.
I found this () clever solution, but it has a few flaws:
staying_in_site = false;
Event.observe(document.body, 'click', function(event) {
if (Event.element(event).tagName == 'A') {
staying_in_site = true;
}
});
window.onunload = popup;
function popup() {
if(staying_in_site) {
return;
}
alert('I see you are leaving the site');
}
It displays the message also when refreshing the page or using the back button.
Do you know a better solution or how to fix it in the above code? I'm no javascript master :)
My intention is to add the code on very specific landing pages only, and display the message when people leave the page without downloading my trial software or reading other pages on my site.
I'm looking for a way to display a message to the user if he leaves my site after only viewing one page.
I found this (http://www.pgrs/2008/1/30/popup-when-leaving-website) clever solution, but it has a few flaws:
staying_in_site = false;
Event.observe(document.body, 'click', function(event) {
if (Event.element(event).tagName == 'A') {
staying_in_site = true;
}
});
window.onunload = popup;
function popup() {
if(staying_in_site) {
return;
}
alert('I see you are leaving the site');
}
It displays the message also when refreshing the page or using the back button.
Do you know a better solution or how to fix it in the above code? I'm no javascript master :)
My intention is to add the code on very specific landing pages only, and display the message when people leave the page without downloading my trial software or reading other pages on my site.
Share Improve this question edited May 2, 2010 at 14:57 skaffman 404k96 gold badges824 silver badges775 bronze badges asked May 2, 2010 at 14:55 JubbaJubbaJubbaJubba 3772 gold badges6 silver badges14 bronze badges 4- 10 I do not want to help you do this!!! – Galen Commented May 2, 2010 at 14:59
- 7 Please don't do this, so annoying! – Oskar Kjellin Commented May 2, 2010 at 15:02
- display the message when people leave the page without downloading my trial software or reading other pages on my site Yeah, most (smart) users flag those sites as "dangerous" on WOT. – nc3b Commented May 2, 2010 at 15:03
- @Brian, consider what is going on in the mind of a user when they decide to leave your website without downloading your trial software. They have already decided your software isn't for them at that particular time, and now you're going to throw up a dialog asking them to reconsider? Do you realize how insanely rude and infuriating that type of message is? Trust me - if people aren't downloading your software now, they sure as hell won't download it when you implement something like this. Be nice to your users. Don't be a jerk. – Rob Commented May 2, 2010 at 15:47
3 Answers
Reset to default 6I will start by saying that I will not, in any way, remend that you do this. It's a bad practice, it only annoys users and it makes you look extremely desperate. I repeat, don't do this, it's not a good idea, it's silly and it's not a good idea.
But the way you can do it is to only display the message when users are clicking on links that lead away from the site.
You can do this by looking at the href
attribute of the link and check if it's an external site. If it is, then display the message
But I repeat, don't do this!
We might be able to forgive you for window.onbeforeunload
. But that's it!
There are some cases where this can be very useful and not having this feature will be annoying. One of the applications I developed for a client required users to input a lot of data on a page (close to 25 fields). I know it would have been better and more usable if the form was broken into more pages, but that's not the point here. There were instances where employees (mostly not very savvy puter users) entered data but for whatever reason left the page without submitting (accidentally, I am sure), resulting in frustration. I was happy to do window.onbeforeunload
in that case.
本文标签: javascriptDisplay message when user leaves siteStack Overflow
版权声明:本文标题:javascript - Display message when user leaves site - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745653482a2161457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论