最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - changing page in HTML - Stack Overflow

programmeradmin2浏览0评论

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 be post or get, not confirm. 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
Add a ment  | 

7 Answers 7

Reset to default 1

There 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 GETor 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>
发布评论

评论列表(0)

  1. 暂无评论