Publishing System Settings Logout Login Register
Redirect your browser on user acceptance
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on May 5th, 2007
4347 views
JavaScript
This tutorial is intended for Intermediate JavaScript users, basic knowledge of JavaScript is recommended.
This is the third of five JavaScript redirection tutorials I will be writing.

The files used in this tutorial are available for download. (.zip)


Table of Contents
1 - Introduction
2 - Confirm Function
3 - Conclusion

- Introduction
In this tutorial you will learn how to give the user a choice to redirect or not to, using a confirm function, an if & else statements.

Lets get started.



- Confirm Function

The Confirm Function is a pop-up window that has an OK button and a CANCEL button. We will be using the OK button to redirect and the CANCEL button to deny the redirection.

picture of the confirm box.


Lets start with the confirm box.
confirm("Would you like to redirect to the a page?")

The confirm function is a simple function, you can replace the text inside the quotes to whatever you want your message to be.

Now lets add a IF & ELSE statement to it.
if(confirm("Would you like to redirect to the a page?"))
{

} else {

}

The IF statement will control the ok button, and the ELSE statement will control the cancel button.

Now we will use the location property for our redirection. (for more information on the location property see this tutorial.)
if(confirm("Would you like to redirect to the a page?"))
{
location = "http://www.mysite.com";
} else {

}

Our script now says : If the OK button is pushed redirect to http://www.mysite.com.

Now lets tell it what to do when we click cancel.
if(confirm("Would you like to redirect to the a page?"))
{
location = "http://www.mysite.com";
} else {
history.back()
}

We used the history.back() function as our else statement, this tells our browser that we don't want to redirect to the next page, and cancels the redirection.

Now lets put this into a simple HTML page.
<html>
<head>
<script type="text/javascript">
if(confirm("Would you like to redirect to the a page?"))
{
location = "http://www.mysite.com";
} else {
history.back();
}
</script>
</head>
<body>
</body>
</html>




- Conclusion

In this tutorial you learned how to let the user decide if they want to redirect, using If, ELSE, LOCATION, and a CONFIRM function. The files used in this tutorial are available for download.

You can also include the JavaScript in an external file by using <script type="text/javascript" src="myjavascript" /> Both ways are included in the file download.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
David Knight

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top