Solved

onunload - check new location to be loaded

Posted on 2001-07-10
19
252 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:OTPeter
  • 8
  • 6
  • 2
  • +2
19 Comments
 
LVL 2

Expert Comment

by:djokov
ID: 6269052
listening...
0
 
LVL 2

Expert Comment

by:djokov
ID: 6269108
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
 
LVL 2

Expert Comment

by:mgaurav
ID: 6269148
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:djokov
ID: 6269166
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
 
LVL 2

Expert Comment

by:mgaurav
ID: 6269184
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
 
LVL 2

Expert Comment

by:djokov
ID: 6269217
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
 

Author Comment

by:OTPeter
ID: 6269227
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
 
LVL 19

Expert Comment

by:webwoman
ID: 6269258
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
 
LVL 2

Expert Comment

by:djokov
ID: 6269270
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
 

Author Comment

by:OTPeter
ID: 6269291
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
 

Author Comment

by:OTPeter
ID: 6269292
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
 
LVL 2

Expert Comment

by:djokov
ID: 6269314
i know what i use...
you could ADD this "onclick='this.href;'" on every link in the document with small javascript...
0
 
LVL 2

Expert Comment

by:djokov
ID: 6269318
"onclick='a=this.href'" i mean
0
 

Author Comment

by:OTPeter
ID: 6269330
aha ...

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

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 150 total points
ID: 6269512
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6269522
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
 

Author Comment

by:OTPeter
ID: 6269563
wow - thats it mplungjan!

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

thanks
peter
0
 
LVL 2

Expert Comment

by:djokov
ID: 6269854
:-)))
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
 

Author Comment

by:OTPeter
ID: 6269905
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question