onunload - check new location to be loaded

hi,

does anybody knwo if it is possible with javascript to check (with the onunload-event) the new loaction which is loaded when a page is unloaded.

the reason is to inform users that the are leaving a plattform, but i do not have the chance to check the links used on any page because they resp. the content can be set be external admins. If i could check i would use javascript functions to open new pages, but unfortunately this is not possible.

so what i would need would be something like this:


<html><head>
     <script language="javascript">
          function message() {
               if window.NEXTLOCATION.host != "www.myDomain.com" alert("You are leaving my plattform!");
          }
     </script>
</head>
<body onunload="message()">
<a href="http://www.foreignDomain.com">leave ...</a>
</body></html>

thanks for your help!

greetings
peter
OTPeterAsked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
Djokov, please be a little less agressive - you are right and mgaurav is right.

Under the constraints of OTPeter's situation you can do this (better give the points to Djokov ;-)

<html>
<head>
     <title>Untitled</title>
  <script>
  function myclick() {
  alert(this.href.toLowerCase());
     if (this.href.toLowerCase().indexOf('http')==0)
        return (confirm('Leave my site?'));
  }
  function changeclick() {
     for (i=0;i<document.links.length;i++)
        document.links[i].onclick=myclick;
  }
  window.onload=changeclick;
 
  </script>
 
 
</head>

<body>
<a href="page2.html">Stay</a>
<a href="http://www.irt.org/">Leave</a>


</body>
</html>



0
 
djokovCommented:
listening...
0
 
djokovCommented:
This is:

<html><head>
    <script language="javascript">
         var a;
         function message() {
              alert('unload '+a);
         }
    </script>
</head>
<body onunload="message()">
<a href="http://www.foreignDomain.com" onclick="a=this.href;">leave ...</a>
</body>
</html>
Works in IE, but I'm not sure if netscape 4.x supports alert in onUnload events.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
mgauravCommented:
NO,
 There is NO method to tell which page the user is going to, by using JS. That's not supported, and in my opinion should never be supported (SECURITY PRBs).

Gaurav
0
 
djokovCommented:
YES, if user clicks on link on your page you could find what is the href of the next site. Just check my script.
0
 
mgauravCommented:
NO,
 There is NO method to tell which page the user is going to, by using JS. That's not supported, and in my opinion should never be supported (SECURITY PRBs).

Gaurav
0
 
djokovCommented:
mqaray,
just check this script - it will alert with the next page if you click on some link on the page:
<html><head>
   <script language="javascript">
        var a;
        function message() {
             alert('You are going to: '+a);
        }
   </script>
</head>
<body onunload="message()">
<a href="http://www.foreignDomain1.com" onclick="a=this.href;">leave to foreignDomain1...</a>
<a href="http://www.foreignDomain2.com" onclick="a=this.href;">leave to foreignDomain2...</a>
</body>
</html>
0
 
OTPeterAuthor Commented:
djokov: the problem on your solution is that i have NO CONTROL about the links in the pages! they are set by administrators and i cant be sure that the use the events i want them to!

the only thing i can do, if possible, is to check onunload where the user is going without getting any parameters from the links onclick.

so lsadly i think mqaurav is right ...

or does anybody else know a solution ???
0
 
webwomanCommented:
Once they leave the page, YOU can't check ANYTHING -- they're not there anymore.

And all you can check is that the page is UNLOADING -- you can't check where they're going because you don't know that until the page unloads and once it's unloaded your script is too.

Now, if you needed to check where they came FROM, that's easy. Where they're going TO isn't.
0
 
djokovCommented:
If you were a little bit smarter you would find the next step in my script that will give you the solution... but you are just rough...
0
 
OTPeterAuthor Commented:
djokov: you use onclick in the <a>-tag. but i have NO CONTROL about the whole <body> and so i dant set this events - admins can do there what they want and use links they want. i cant be sure that they use any events or functions when they set links.

0
 
OTPeterAuthor Commented:
djokov: you use onclick in the <a>-tag. but i have NO CONTROL about the whole <body> and so i dant set this events - admins can do there what they want and use links they want. i cant be sure that they use any events or functions when they set links.

0
 
djokovCommented:
i know what i use...
you could ADD this "onclick='this.href;'" on every link in the document with small javascript...
0
 
djokovCommented:
"onclick='a=this.href'" i mean
0
 
OTPeterAuthor Commented:
aha ...

counds good. but how should this small script look like? i have no idea ...

0
 
Michel PlungjanIT ExpertCommented:
PS: The script will ONLY work on links on the page.
You will need to modify the onLoad and onUnload to handle urls typed into the location bar, dropped on the page or bookmarks loaded. Also closing the browser is not going to do anything.

Michel
0
 
OTPeterAuthor Commented:
wow - thats it mplungjan!

great script!
of course you'll get the points ...

thanks
peter
0
 
djokovCommented:
:-)))
easy guys,
i don't care about points...
this is the decision i had in my mind:
<html><head>
  <script language="javascript">
       var a="nowhere";
       function message() {
        //alert(event.target);
            alert('You are going to: '+a);
       }

       function add() {
            for (i=0;i<document.links.length;i++)
                 document.links[i].onclick= new Function("a=document.links['"+i+"'].href;");
       }
  </script>
</head>
<body onunload="message()" onload="add()">
<a href="http://www.foreignDomain1.com">leave to foreignDomain1...</a>
<a href="http://www.foreignDomain2.com">leave to foreignDomain2...</a>
</body>
</html>
0
 
OTPeterAuthor Commented:
sorry djokov,

but this adding of "onclicks" was the crucial part and i didnt know how to do.

hope you really dont care about points ...

thanks for your help too!!!

greetings
peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.