This tutorial is based around making a simple confirm box.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<header>
<script language="javascript" type="text/javascript">
</script>
</header>
<body>
<p>Use the button to display a confirm box.</p>

<button onclick="confirmBox()">Click</button>

<p id="confirm"></p>

<script>
function confirmBox()
{
var x;
var r=confirm("Press a button!");
if (r==true)
  {
  x="You pressed OK!";
  }
else
  {
  x="You pressed Cancel!";
  }
document.getElementById("confirm").innerHTML=x;
}
</script>

</body>
</html>

As you can see here that you have used a pop up function and applied it to a button with a text return for a answer. But what if we want to change the o.k/cancel buttons to redirection back to the home page or store.

if (r==true)
  {
  x="You pressed OK!";
  }
else
  {
  x="You pressed Cancel!";
  }

in between the brackets we have conditions for what is to happen and right now they are set to phase text as a return.

by using a "window.location" function we can request the active window to go to a location.

x=window.location = "page1.html";

Your end result should look like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<header>
<script language="javascript" type="text/javascript">
</script>
</header>
<body>
<p>Click the button to display a confirm box.</p>

<button onclick="confirmBox()">Click</button>

<p id="confirm"></p>

<script>
function confirmBox()
{
var x;
var r=confirm("Do you agree to enter!");
if (r==true)
  {
  x=window.location = "page1.html";
  }
else
  {
  x=window.location = "page2.html";
  }
document.getElementById("confirm").innerHTML=x;
}
</script>

</body>
</html>

Now that you have the building blocks you can alter the code and bind it to your needs.

This page was published on It was last revised on

0

2 Comments

  • Votes
  • Oldest
  • Latest
Commented
Updated

Thank you, it was helpful to me.

add a comment
0
Commented
Updated

Thank you, it was helpful to me.

np at all, should be enough to show you how to apply the function.

add a comment
0

User

Community

Market

Help Center

Legal

Company

Social Media

\n\n\n

Use the button to display a confirm box.

\n\n\n\n

\n\n\n\n\n\n```\n\nAs you can see here that you have used a pop up function and applied it to a button with a text return for a answer. But what if we want to change the o.k/cancel buttons to redirection back to the home page or store.\n\n```\nif (r==true)\n {\n x=\"You pressed OK!\";\n }\nelse\n {\n x=\"You pressed Cancel!\";\n }\n```\n\nin between the brackets we have conditions for what is to happen and right now they are set to phase text as a return.\n\nby using a \"window.location\" function we can request the active window to go to a location.\n\n```\nx=window.location = \"page1.html\";\n```\n\nYour end result should look like this\n\n```\n\n\n
\n\n
\n\n

Click the button to display a confirm box.

\n\n\n\n

\n\n\n\n\n\n```\n\nNow that you have the building blocks you can alter the code and bind it to your needs.","url":"https://www.ozzu.com/wiki/595399/javascript-confirm-box","datePublished":"2012-09-15T20:29:31+00:00","dateModified":"2012-10-20T12:07:29+00:00","author":{"@type":"Person","name":"Zealous"},"interactionStatistic":[{"@type":"InteractionCounter","interactionType":"https://schema.org/LikeAction","userInteractionCount":0},{"@type":"InteractionCounter","interactionType":"https://schema.org/ViewAction","userInteractionCount":5431}],"comment":[{"@type":"Comment","url":"https://www.ozzu.com/wiki/595399/javascript-confirm-box#post-595827","datePublished":"2012-10-19T01:57:20+00:00","dateModified":"2024-07-16T17:17:26+00:00","author":{"@type":"Person","name":"Austin80ss"},"text":"Thank you, it was helpful to me.","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/LikeAction","userInteractionCount":0}},{"@type":"Comment","url":"https://www.ozzu.com/wiki/595399/javascript-confirm-box#post-595846","datePublished":"2012-10-20T05:07:29+00:00","dateModified":"2024-07-16T17:17:26+00:00","author":{"@type":"Person","name":"Zealous"},"text":"> Thank you, it was helpful to me.\n\nnp at all, should be enough to show you how to apply the function.","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/LikeAction","userInteractionCount":0}}]}