A javascript confirmation box can be a handy way to give your visitors a choice of whether or not an action is performed. A confirmation box will pop up much like an alert box, but will allow the viewer to press an "OK" or "Cancel" button. Here is the basic command for creating a confirmation box:
| confirm("Text or question you want to use"); |
The trouble is, if you use just that it isn't very useful. Theconfirmation box will return a value of true or false, so this is whatwe must use to make use of the confirmation box. An easy way to get thevalue for later use is to assign it to a variable, like this:
| var where_to= confirm("Do you really want to go to this page??"); |
Now, you can use the where_to variable to send the user to one page or another, depending on the value the confirmation box returned. You can do this with an if/else block:
| if (where_to== true) { window.location="http://yourplace.com/yourpage.htm"; } else { window.location="http://www.barbie.com"; } |
In this case, if the viewer hits the "OK" button, the browser willgo to your special page. If the viewer hits the cancel button, thebrowser takes the viewer on a fun trip to the Barbie web site!
To make use of it though, you'll probably want to write a JavaScript function in your head section that you can call in the body of the page. You could place the items above into a function like this, to go inside your head tags:
| <SCRIPT language="JavaScript"> <!-- function go_there() { var where_to= confirm("Do you really want to go to this page??"); if (where_to== true) { window.location="http://yourplace.com/yourpage.htm"; } else { window.location="http://www.barbie.com"; } } //--> </SCRIPT> |
Now, you need to find a way to call the function inside the body of your document so that you can offer the viewer access to the special page. You can use a button to call the function when it is clicked, so you would place something like this in the body section:
| To go to my special page, click the button below:<BR><FORM><INPUT TYPE="button" value="Go!" onClick="go_there()"></FORM> |
This would work like the example button, give it a try!
As you can see, this may come in handy for something you want to code down the line. Of course, I can't confirm that...
Well, that does it for now. Let's go check out the next section, Browser Detection with JavaScript.
Related Articles
- Create User Friendly Confirmation PagesIncluding a form on your website is the best way to get feedback from your visitors. You can have a form to gather your visitors email address so that they can subscribe to your ezine. Or you can have a form to collect visitors comments about your site. Also, forms allow your visitors to send.....
- JavaScript Browser DetectionBrowser detection allows you to find out what browser your viewer is using, and then perform a script based on it - or just to send a friendly message to those with your favorite browser.
- JavaScript AlertsWell, you want to add one of those JavaScript alert boxes that come out of nowhere, dont you? Okay, lets begin with the alert box that just tells the viewer something you want them to know. Heres the alert command:
- Creating AlertsA simple guide to using alerts (and annoying your visitors)
- The Basics- JavaScript TutorialTo get started with JavaScript, you will want to be able to see the tag that will set a script apart from the HTML. The tags used to begin and end a script are the tags. The opening tag should appear like this
- 4 Powerful Features of Javascript Programming LanguageIn the community of web developers and surfers, Javascript is highly popular as client side scripting language for web browsers. Lets take a look at some of the features of this language.
- Professional Web Host LayoutThis is no doubt my longest tutorial yet, so I suggest you save this every few minutes incase you loose it. (ie. computer freeze, which everyone hates)
- JavaScript PromptsWell, lets say you wanted to get somebodys name before they saw the page, and then write their name on your page right before their very eyes... Well, you can do this using a javascript prompt. Heres the command:
- Containment ExplainedContainment is the effect where one or more elements is shown as part of a group or category, through a visual mechanism. There are two variations of mechanism: strict encapsulation, where the element is totally contained within a box; and softer containment...
- Introduction to Javascript for NewbiesJavascript is a client-side programming language whose processing engine is embedded in web browsers like Internet Explorer, Netscape, Firefox, etc. This enables the processing engine to read and interpret web pages that contain the javascript code when browsing
