Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can javascript open window when user leaves *url* ?

Status
Not open for further replies.

nyc2001

Programmer
Jan 17, 2002
7
US
Hello,

I know that javascript can detect a user leaving a page
(or refresh) with onunload

That's fine

However, how can one use javascript (if possible)
to detect when the *url* itself is exited?

imagine an onunload that *only works when the url, not just the page* is exited. This isn't possible, is it?

My only idea to do this was use a frame, have one page hidden as a server side scripted page, use a querystring with the url appended, and when that qstring <> the url, it will know through an if/then condition, and pop up the window

I was hoping that isn't needed though, so can I just do this with javascript? or is my server side method the only way?

Thanks
 
No, like I stated, I cannot just use a typical onunload

again, those scripts work when you refresh and just leave a page

I mean knowing the actual DOMAIN was exited

thanks anyway

joel
 
It can be done but might require some extensive changes to your current web site.

To simplify my explanation, take a single web page and initialize a variable globally when the page loads. Then setup a single function to process all of the page navigation within your web page. Remember, all navigation must point to this function. Then setup an onUnload event handler that will examine whether or not the reason the page is being unloaded is a result of navigation within your site or navigation to another site or closing the browser. Something like this.
Code:
<head>
<script Language=&quot;JavaScript&quot;>
<!--
var mySite = 0

function nextPage(pageRef) {
  mySite = 1
  window.location = pageRef
}

function dontGo() {
  if(mySite == 0) {
    // Do Something Quick !!  The user is leaving your site.
  }
}

//-->
</script>
</head>

<body onUnload=&quot;dontGo();&quot;>
<a href=&quot;javascript:nextPage('somepage.html');&quot;>Go to some page within my site.</a><br>
<input type=&quot;button&quot; value=&quot;Go&quot; onClick=&quot;nextPage('someotherpage.html');&quot;>
</body>
Does that make any sense ?? Might require some significant changes to your existing site but should work like a charm !! :)

ToddWW
 
Here's how it could be done:

You must put this function and onunload event on every single page of your site.

function leavingSite(){
//here's where you capture the location the user has just
//entered.
var addr = window.location;
var mySite = &quot;
//Parse the URL
addr = addr.substr(...,...);
//compare the site names
if (addr != mySite){
window.open();
}

}

<Body onunoload=&quot;javascript:leavingSite()&quot;> Rocco is the BOY!!

M O T I V A T E!
 
Rocco,

Doesn't that var address = window.location read the URL of the page you're leaving or does it extract the URL of the page you're going to ??

ToddWW
 
I believe that as soon as you push enter the location object becomes either what you typed or pushed the back button to. That may be the only sticking point if it is not the case, only way to tell is to test it.

Correction:

var addr = window.location.href.hostname;
var mySite = &quot; &quot;;

if (addr != mySite){.... Rocco is the BOY!!

M O T I V A T E!
 
Darn it,

correction:

var addr = window.location.hostname;

There that should do it. Rocco is the BOY!!

M O T I V A T E!
 
THanks guys

I will test this out

I really appreciate it

Joel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top