March 9, 2013
Opening a new page on website exit
Question by user2152403
What I want is to have a new page or pop up to be activated when:
- a visitor on my website closes the browser
- a visitor on my website types another URL in his browser.
I have this JS, but it is activating a new website (www.newpage.com) also als for internal links. How can I disable this action for internal links?
<script type="text/javascript">
var leaving = true;
function checkUrl(href) {
leaving = (((href.indexOf("http://") > -1) || (href.indexOf("www.") > -1)) && (href.indexOf(window.location.host) == -1));
}
function pop() {
if(leaving) window.open("http://www.newpage.com");
}
window.onunload = pop;
var e = documents.getElementsByTagName("a");
for(var i=0;i<e.length;i++) e[i].onclick = "checkUrl(this.href)";
</script>
Answer by Starx
How can I disable this action for internal links?
Use specific links with id
instead of getting all the anchors based on tag name.
var e = documents.getElementById("anchorId");
e.onclick = "checkUrl(this.href)";
Also, Listens to the comments. POPUPs are the big NO is current web world
Opening a new window when a user is actually trying to leave??? This may have the tendency to the make the viewer angry and never return on the site. BAD MOVE