DelphiFAQ Home Search:
General :: Web publishing :: JavaScript
Code snippets to add functionality to your HTML pages, preferably cross-browser.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

How can I prevent the user from typing in a URL

Question:

My web pages's JavaScript needs to be notified (event) when the user types a URL in the browser's address field (planning to leave the site?). How can I do this?

Answer:

It's not possible. The browsers location/address bar is not a JavaScript object, so there are no JavaScript methods for it.

However when someone comes to your site you could check for the window's name. If it is not a specific unusual name (which you hard code now), then you know that it is the first visit and the browser probably does display an address bar.
Just use javascript to open a new window (with the same url) and from there close the opening window (window.opener.

<script language=JavaScript>
 <!--
 var wn = window.name.substring(0,6);
 
 if (wn!="my_secret_name_123") {
   window.open(document.location, 
   'my_secret_name_123',
   'location=no');  
 }
 else {
   // close the opener
 
   if (document.opener) {
     document.opener.close;
   }
 }
 //-->
 </script>
 

Generated 16:02:07 on Jan 23, 2017