Ce tutoriel est basé autour de fabrication d'une boîte de confirmation simple.
<!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>
- <!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>
Comme vous pouvez le voir ici que vous avez utilisé un pop-up fonction et appliquée à un bouton avec un texte de retour pour une réponse. Mais que se passe-t-il si nous voulons changer les boutons o.k/cancel de redirection vers la page d'accueil ou à stocker.
if (r==true)
{
x="You pressed OK!";
}
else
{
x="You pressed Cancel!";
}
- if (r==true)
- {
- x="You pressed OK!";
- }
- else
- {
- x="You pressed Cancel!";
- }
entre les crochets, que nous avons des conditions pour ce qui est de se produire et maintenant qu'ils sont définis au texte de phase comme un retour.
en utilisant une fonction de « window.location », nous pouvons demander la fenêtre active d'aller à un endroit.
x=window.location = "page1.html";
Votre résultat final devrait ressembler à ceci
<!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>
- <!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>
Maintenant que vous avez les blocs de construction, vous pouvez modifier le code et les lier à vos besoins.