I wanted to set up a JS when users press the button, it would redirect them to the homepage, which called index.html
<form method="confirm" style="width: 500px; height: 300px; margin-left: auto; margin-right: auto; margin-top:100px; margin-bottom: 100px; font-size: 30px; color: black;">
<fieldset id="confirm">
<legend>NOTIFICATION</legend>
<table>
<tr>
<td>
<h1>THANK YOU FOR SIGNING IN</h1>
</td>
</tr>
<td>
<input type="submit" value="Return to homepage" onclick="return return1();">
<script>
function return1()
{
window.location.href = 'index.html';
}
</script>
</td>
</tr>
</table>
</fieldset>
</form>
I wanted to set up a JS when users press the button, it would redirect them to the homepage, which called index.html
<form method="confirm" style="width: 500px; height: 300px; margin-left: auto; margin-right: auto; margin-top:100px; margin-bottom: 100px; font-size: 30px; color: black;">
<fieldset id="confirm">
<legend>NOTIFICATION</legend>
<table>
<tr>
<td>
<h1>THANK YOU FOR SIGNING IN</h1>
</td>
</tr>
<td>
<input type="submit" value="Return to homepage" onclick="return return1();">
<script>
function return1()
{
window.location.href = 'index.html';
}
</script>
</td>
</tr>
</table>
</fieldset>
</form>
Share
Improve this question
asked Oct 4, 2014 at 8:07
Pé BinPé Bin
4613 gold badges9 silver badges14 bronze badges
3
- when i clicked the button, it does not redirect me to the index page:) – Pé Bin Commented Oct 4, 2014 at 8:09
-
I'm pretty sure
method
attribute can only bepost
orget
, notconfirm
. Look at the difference here: w3schools./tags/att_form_method.asp – Pixeladed Commented Oct 4, 2014 at 8:14 - @PéBin How did you solve it? If some answer helped you out you can accept it so the question can be closed :) – Bojan Petkovski Commented Oct 4, 2014 at 8:31
7 Answers
Reset to default 1There are some mistakes here.
The error you get is because you use a submit input button inside a form and the button will try to evaluate your form submission. To prevent that you have to return false on your function.
<script>
function return1()
{
window.location.href = 'index.html';
return false;
}
</script>
But if you want to do a button that simply will redirect to another page you should not use a form and a submit button but just a normal input button
<input type="button" value="Return to homepage" onclick="return1();">
If you want to use the form because you want to evaluate some data you need to put your page on the <form>
in the action
field, without using the script
Also confirm is not accepted you should use GET
or POST
.
<form method="POST" action="index.html">
Add return false to the function to disable the default event on submit(page refresh)
function return1()
{
window.location.href = 'index.html';
return false;
}
The following code redirects me to index.php which sits at the same level as index.html and index.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="check.php" method="post" id="re">
<fieldset id="confirm">
<legend>NOTIFICATION</legend>
<table>
<tr>
<td>
<h1>THANK YOU FOR SIGNING IN</h1>
</td>
</tr>
<td>
<input type="submit" value="Return to homepage">
</td>
</tr>
</table>
</fieldset>
</form>
<script src="index.js"></script>
</body>
</html>
The JS file
function _x(elem){
return document.getElementById(elem);
}
var x = _x("re");
x.onsubmit = function(e){
e.preventDefault();
window.location = "index.php";
}
simply change your input type submit
to button
<input type="button" value="Return to homepage" onclick="return return1();">
Replace
window.location.href = 'index.html';
with:
window.open('INDEX.HTML');
You change code
<input type="submit" value="Return to homepage" onclick="return return1();">
=> <a href="index.html" class="btn">Return to homepage</a>
With you type css ".btn" see like a button
Note : In form, you should not put or to redirect without submit form.
This may help you
<script language="javascript">
function return1()
{
window.location="yourpage.html";
}
</script>