Link to home
Start Free TrialLog in
Avatar of OTPeter
OTPeter

asked on

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
Avatar of djokov
djokov

listening...
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.
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
YES, if user clicks on link on your page you could find what is the href of the next site. Just check my script.
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
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>
Avatar of OTPeter

ASKER

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 ???
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.
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...
Avatar of OTPeter

ASKER

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.

Avatar of OTPeter

ASKER

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.

i know what i use...
you could ADD this "onclick='this.href;'" on every link in the document with small javascript...
"onclick='a=this.href'" i mean
Avatar of OTPeter

ASKER

aha ...

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

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of OTPeter

ASKER

wow - thats it mplungjan!

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

thanks
peter
:-)))
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>
Avatar of OTPeter

ASKER

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