/    Sign up×
Community /Pin to ProfileBookmark

Javascript onUnload

Hello,

Ok so I have searched for weeks now and haven’t found exactly what I am looking for yet so I decided to try and ask in this forum.

So I need a pop-up to be displayed when someone tries to exist a particular page on a website (I dont want to do this my boss made me, I hate pop-ups)

The problem is if they hit the back button or refresh button and they are still on our site the pop-up gets displayed. We only want it to appear if they exit our domain. If anyone knows how to do this I would greatly appreciate your help. Thanks in advance, below is the code that I have so far.

<html>

<head>

<title></title>

<script language=”JavaScript”>
<!–
ok=1
function open_on_departure(url,name)
{
if (ok)
popupWin = window.open(‘http://www.example.com/pop-up.html’,’pop_up‘, ‘ width=250,height=300,left=100,top=100’)
}
// –>
</script>

</head>

<body onLoad=”leaving=true” onUnload=”if (leaving) open_on_departure()”>

</body>
</html>

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@WebJoelJul 14.2008 — I don't know alot of javascript, but maybe this will help:

http://www.irt.org/script/1624.htm

<body onLoad="leaving=true" [B]onUnload="if (leaving) window.open('popup.htm','windowName');[/B]">....[/QUOTE] seems different than your approach..
Copy linkTweet thisAlerts:
@Danpaul01authorJul 14.2008 — Thanks for the quick reply, but it didnt work. I can get the popup to appear if they leave the site, but the problem is if they are on my site and click the refresh button on their browser it appears, also if they are on my homepage and go to a subpage and click back on their browser it also appears. So I need some javascript in there that checks if they exit my domain then to display the pop-up.
Copy linkTweet thisAlerts:
@felgallJul 14.2008 — Popup blockers will block the opening of a window on that event. It is the main event that the blockers are set up to block - to prevent attempting to close one site from popping up one or more others.
Copy linkTweet thisAlerts:
@Danpaul01authorJul 14.2008 — Yeah I know, I told my boss that all current web browsers have pop-up blockers and that the ones that dont will probably just bug our customers and never have them return. But he is still set on doing this and since he writes the checks I gotta try and produce what he wants. If anyone had done this or knows how I would still greatly appreciate it if you let me in your secret. Thanks in advance.
Copy linkTweet thisAlerts:
@sublime54321Jul 14.2008 — Sometimes our jobs include telling the boss when they are wrong. It's not always the easiest thing to do, unless your boss is a complete idiot you will be better respected for telling him now as opposed to later on when all your customers have abandoned the site.

My company attempted something similar to this once, and promptly removed it from the site within DAYS when it was discovered how badly it was affecting traffic.

Be prepared to back up your statements with research indicating why this is incorrect.
Copy linkTweet thisAlerts:
@Danpaul01authorJul 14.2008 — thanks for the encouraging words, but if you only knew my boss, lol. well if anybody else still knows how i would still greatly appreciate it. but thanks for all the replys so far, much appreciated.
Copy linkTweet thisAlerts:
@Danpaul01authorJul 14.2008 — If this helps I found this today, but cant get it to work right.



<script type="text/javascript">

var leaving = true;

function checkUrl(href) {

leaving = (((href.indexOf("://") > -1) || (href.indexOf("www.") > -1)) && (href.indexOf(window.location.host) == -1));

// add in more things to include in the line above as you see fit

}

function pop() {

if(leaving) window.open("popup.html");

}

window.onunload = pop;

var e = documents.getElementsByTagName("a");

for(var i=0;i<e.length;i++) e[i].onclick = "checkUrl(this.href)";

</script>

</head>
×

Success!

Help @Danpaul01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...