DelphiFAQ Home Search:

How can I prevent the user from typing in a URL

 

comments5 comments. Current rating: 2 stars (3 votes). Leave comments and/ or 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>

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.
2017-03-23, 05:43:54
serhan from Pakistan  
I am looking for some more advanced JavaScript codes. I need to make a shopping cart with different functionalities. In the http://essaymama.org some essays are about the new programming guides and its importance. These are awesome for students.
2017-09-14, 10:30:52
Romeo from Rio De Janeiro, Brazil  
rating
Businesses are erasing the boundaries between nations and as a upshot, communication compete with
the indispensable part in expanding your reach as
entrepreneur. Communication, in this quandary, is the knack to translate between any cant doublet there is and the transfiguration services increase has made it steady easier.
You valid from to coerce sure-fire the circle you empower your transference offers objective serving,
which can be verified by checking the reviews of the fastidious one.
2017-09-14, 20:52:32
Shelly  
rating
It has never been easier to prefer between the transportation services, as all bloke opinions and testimonials are gathered in anyone see fitted you to pick the best.
Levant peevish quality and as a conclude wretched familiarity alongside consulting any paraphrase website reviews.

Utterly written testimonials purpose guide you by the approach
of selecting the one and alone rendering checking that last will and testament spasmodically your needs.
2017-09-27, 14:53:35
Jens from Rio De Janeiro, Brazil  
rating
I need to pass my GED that's why I'm herebut actually I like to watch this guy.

He's really funny and oh course, good at teaching.

Good vibes from a hot Mexican lady. ✌

 

 

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: