faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 27 people (26%) answered Yes
Recently 1 of 10 people (10%) answered Yes



Apr 8th, 2008 18:07
ha mo, joy chatterjee, Colin Fraser, Becky Smiths,

Your question is not very clear, but there are a couple of things that 
come to mind. The first question is to ask why do you want to replace 
the next url the user wants to go to with something different? I am 
sure that this would be somewhat annoying to a lot of users so would 
not necessarily be a good idea. 
It may be better to use this instead : 
In the head :
<script language="JavaScript"><!--
   function doThis() {
      closingWindow = window.open('','closeWin','width=100,height=100,
      closingWindow.location.href = 'closure.html';
Which opens a window that you can use to say"Goodbye" or whatever
<body onUnload="doThis()"> 
This calls the window opener when the page is unloaded. 
There is also the replace() method, but I would suggest this is asking 
for trouble. 
<script language="JavaScript"><!--
 function goMyurl() {
     if (document.images)
      location.href = 'http://www.this.org/place/is/cool.htm';
<body onUnload="goMyurl()">
Replace() will work across all browsers that I have tested, but Opera 
only uses absolute URL's. 
THe only issue for you is still why do you want to inerfere with the 
user's determination to go somewhere else? You want to make your page 
gluey, that is ensure all users are stuck in your page? Or is it that 
you want to display some sort of advertisement or message when users 
leave your site? This is a much more appropriate use of Javascript than 
many other pages try, just be careful with it. 
The only other thing that comes to mind from your question is that you 
want to prompt the user for a URL at some stage in your page and then 
you want the browser to be directed to that URL when the current page 
unloaded. There is a number of problems here, not the least of which is 
that few users would even be aware of what URL they want to go to next. 
Be that as it may, you could call a prompt and take the url from that 
the unload event, pretty much as the first example above:  
<script language="JavaScript"><!--
   function doThis() {
      var url = prompt ("What URL do you want","Write it here");
     if (document.images)
      location.href = url;
Good luck.