Solved

onunload - check new location to be loaded

Posted on 2001-07-10
19
246 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now