If you have a page which deletes records from a database, or indeed have any kind of link that you want the user to be doubly certain that they 'mean what they click', using the Javascript confirm event handler is a simple and effective way to get the job done. Here's the effect we're going to achieve:
Click here to return home
Assuming you're still with us and didn't after all return to the home page, you can see that the confirmation makes doubley sure you're aware of what you're doing. (Apparently some sites also use it for asking 'Are you over 18'...)
Now let's look at the script:
|
Keep all the above on one long line to ensure it works properly.
The key to this code is the phrase return confirm. Just putting confirm by itself wouldn't do much, we have to tell the browser what to do with it's confirmation. Once we tell it to return the users selection (either true for Yes or False for No), the code now simply returns false -- does nothing -- if a No is selected, and if it's true -- a Yes response -- completes the link command and zooms off to the delete page or whatever - Magic!
No comments:
Post a Comment