DelphiFAQ Home Search:

How can I prevent the user from typing in a URL

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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>

Content-type: text/html

Comments:

2009-01-21, 08:29:35   (updated: 2009-01-21, 08:30:24)
wpezoa@gmail.com from Chile  
Excelente codigo, sirve para que no entren usuarios por url a paginas restringidas, cuando las páginas tienen frames y popus.
saludos, regards.

Wladimir.

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: