JavaScript window.location.href code not working











up vote
-1
down vote

favorite












I know this question has been asked a lot before, but none of the fixes worked for me. I made a window.location. href thing before and that worked but this one does not. I know the function runs because I tested it with an alert. Can someone see anything wrong here?



<form>

<input type="submit" name="agree" value="agree" onclick="fagree()">
<input type="submit" name="disagree" value="Decline and go to google" onclick="fdisagree()">

</form>

<script type="text/javascript">
function fagree(){
window.location.assign("index.html")
localStorage.setItem("Terms", "true")
}
function fdisagree(){
window.location.href="https://www.google.com/"
return false;
}
</script>
</body>









share|improve this question




























    up vote
    -1
    down vote

    favorite












    I know this question has been asked a lot before, but none of the fixes worked for me. I made a window.location. href thing before and that worked but this one does not. I know the function runs because I tested it with an alert. Can someone see anything wrong here?



    <form>

    <input type="submit" name="agree" value="agree" onclick="fagree()">
    <input type="submit" name="disagree" value="Decline and go to google" onclick="fdisagree()">

    </form>

    <script type="text/javascript">
    function fagree(){
    window.location.assign("index.html")
    localStorage.setItem("Terms", "true")
    }
    function fdisagree(){
    window.location.href="https://www.google.com/"
    return false;
    }
    </script>
    </body>









    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I know this question has been asked a lot before, but none of the fixes worked for me. I made a window.location. href thing before and that worked but this one does not. I know the function runs because I tested it with an alert. Can someone see anything wrong here?



      <form>

      <input type="submit" name="agree" value="agree" onclick="fagree()">
      <input type="submit" name="disagree" value="Decline and go to google" onclick="fdisagree()">

      </form>

      <script type="text/javascript">
      function fagree(){
      window.location.assign("index.html")
      localStorage.setItem("Terms", "true")
      }
      function fdisagree(){
      window.location.href="https://www.google.com/"
      return false;
      }
      </script>
      </body>









      share|improve this question















      I know this question has been asked a lot before, but none of the fixes worked for me. I made a window.location. href thing before and that worked but this one does not. I know the function runs because I tested it with an alert. Can someone see anything wrong here?



      <form>

      <input type="submit" name="agree" value="agree" onclick="fagree()">
      <input type="submit" name="disagree" value="Decline and go to google" onclick="fdisagree()">

      </form>

      <script type="text/javascript">
      function fagree(){
      window.location.assign("index.html")
      localStorage.setItem("Terms", "true")
      }
      function fdisagree(){
      window.location.href="https://www.google.com/"
      return false;
      }
      </script>
      </body>






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 23:18









      halfer

      14.3k758107




      14.3k758107










      asked Nov 22 at 12:54









      Gard

      31




      31
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can use button type instead of submit, otherwise, well, the form will be submit.



          <form>
          <input type="button" name="agree" value="agree" onclick="fagree()">
          <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">
          </form>

          <script type="text/javascript">
          function fagree(){
          window.location.assign("index.html");
          localStorage.setItem("Terms", "true"); //This line won't be executed, because the above line will reload the page
          }

          function fdisagree() {
          window.location.href="https://www.google.com/"
          return false; //This can be removed because the above line will redirect to google and the return false won't be executed
          }
          </script>


          If you don't need any else form elements than those 2 buttons, you can get ride of the <form></form> and simply use <button>. In example :



          <button type="button" name="agree" value="agree" onclick="fagree()">
          <button type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">





          share|improve this answer






























            up vote
            0
            down vote













            Do not put it in the form if you don't have to (if that's the whole form code in the example). You are submitting it before functions have a chance to trigger.



            OR:



            <form>

            <input type="button" name="agree" value="agree" onclick="fagree()">
            <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">

            </form>

            <script type="text/javascript">
            function fagree(){
            window.location.assign("index.html")
            localStorage.setItem("Terms", "true")
            }
            function fdisagree(){
            window.location.href="https://www.google.com/"
            return false;
            }
            </script>
            </body>





            share|improve this answer























            • ok thx so if i understood that right make the <button> outside a form?
              – Gard
              Nov 22 at 15:06










            • Oh nvm i fixed it by making it a button
              – Gard
              Nov 22 at 15:08











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431513%2fjavascript-window-location-href-code-not-working%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You can use button type instead of submit, otherwise, well, the form will be submit.



            <form>
            <input type="button" name="agree" value="agree" onclick="fagree()">
            <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">
            </form>

            <script type="text/javascript">
            function fagree(){
            window.location.assign("index.html");
            localStorage.setItem("Terms", "true"); //This line won't be executed, because the above line will reload the page
            }

            function fdisagree() {
            window.location.href="https://www.google.com/"
            return false; //This can be removed because the above line will redirect to google and the return false won't be executed
            }
            </script>


            If you don't need any else form elements than those 2 buttons, you can get ride of the <form></form> and simply use <button>. In example :



            <button type="button" name="agree" value="agree" onclick="fagree()">
            <button type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">





            share|improve this answer



























              up vote
              0
              down vote



              accepted










              You can use button type instead of submit, otherwise, well, the form will be submit.



              <form>
              <input type="button" name="agree" value="agree" onclick="fagree()">
              <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">
              </form>

              <script type="text/javascript">
              function fagree(){
              window.location.assign("index.html");
              localStorage.setItem("Terms", "true"); //This line won't be executed, because the above line will reload the page
              }

              function fdisagree() {
              window.location.href="https://www.google.com/"
              return false; //This can be removed because the above line will redirect to google and the return false won't be executed
              }
              </script>


              If you don't need any else form elements than those 2 buttons, you can get ride of the <form></form> and simply use <button>. In example :



              <button type="button" name="agree" value="agree" onclick="fagree()">
              <button type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">





              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                You can use button type instead of submit, otherwise, well, the form will be submit.



                <form>
                <input type="button" name="agree" value="agree" onclick="fagree()">
                <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">
                </form>

                <script type="text/javascript">
                function fagree(){
                window.location.assign("index.html");
                localStorage.setItem("Terms", "true"); //This line won't be executed, because the above line will reload the page
                }

                function fdisagree() {
                window.location.href="https://www.google.com/"
                return false; //This can be removed because the above line will redirect to google and the return false won't be executed
                }
                </script>


                If you don't need any else form elements than those 2 buttons, you can get ride of the <form></form> and simply use <button>. In example :



                <button type="button" name="agree" value="agree" onclick="fagree()">
                <button type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">





                share|improve this answer














                You can use button type instead of submit, otherwise, well, the form will be submit.



                <form>
                <input type="button" name="agree" value="agree" onclick="fagree()">
                <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">
                </form>

                <script type="text/javascript">
                function fagree(){
                window.location.assign("index.html");
                localStorage.setItem("Terms", "true"); //This line won't be executed, because the above line will reload the page
                }

                function fdisagree() {
                window.location.href="https://www.google.com/"
                return false; //This can be removed because the above line will redirect to google and the return false won't be executed
                }
                </script>


                If you don't need any else form elements than those 2 buttons, you can get ride of the <form></form> and simply use <button>. In example :



                <button type="button" name="agree" value="agree" onclick="fagree()">
                <button type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 at 13:07

























                answered Nov 22 at 13:01









                Cid

                3,07421026




                3,07421026
























                    up vote
                    0
                    down vote













                    Do not put it in the form if you don't have to (if that's the whole form code in the example). You are submitting it before functions have a chance to trigger.



                    OR:



                    <form>

                    <input type="button" name="agree" value="agree" onclick="fagree()">
                    <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">

                    </form>

                    <script type="text/javascript">
                    function fagree(){
                    window.location.assign("index.html")
                    localStorage.setItem("Terms", "true")
                    }
                    function fdisagree(){
                    window.location.href="https://www.google.com/"
                    return false;
                    }
                    </script>
                    </body>





                    share|improve this answer























                    • ok thx so if i understood that right make the <button> outside a form?
                      – Gard
                      Nov 22 at 15:06










                    • Oh nvm i fixed it by making it a button
                      – Gard
                      Nov 22 at 15:08















                    up vote
                    0
                    down vote













                    Do not put it in the form if you don't have to (if that's the whole form code in the example). You are submitting it before functions have a chance to trigger.



                    OR:



                    <form>

                    <input type="button" name="agree" value="agree" onclick="fagree()">
                    <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">

                    </form>

                    <script type="text/javascript">
                    function fagree(){
                    window.location.assign("index.html")
                    localStorage.setItem("Terms", "true")
                    }
                    function fdisagree(){
                    window.location.href="https://www.google.com/"
                    return false;
                    }
                    </script>
                    </body>





                    share|improve this answer























                    • ok thx so if i understood that right make the <button> outside a form?
                      – Gard
                      Nov 22 at 15:06










                    • Oh nvm i fixed it by making it a button
                      – Gard
                      Nov 22 at 15:08













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Do not put it in the form if you don't have to (if that's the whole form code in the example). You are submitting it before functions have a chance to trigger.



                    OR:



                    <form>

                    <input type="button" name="agree" value="agree" onclick="fagree()">
                    <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">

                    </form>

                    <script type="text/javascript">
                    function fagree(){
                    window.location.assign("index.html")
                    localStorage.setItem("Terms", "true")
                    }
                    function fdisagree(){
                    window.location.href="https://www.google.com/"
                    return false;
                    }
                    </script>
                    </body>





                    share|improve this answer














                    Do not put it in the form if you don't have to (if that's the whole form code in the example). You are submitting it before functions have a chance to trigger.



                    OR:



                    <form>

                    <input type="button" name="agree" value="agree" onclick="fagree()">
                    <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">

                    </form>

                    <script type="text/javascript">
                    function fagree(){
                    window.location.assign("index.html")
                    localStorage.setItem("Terms", "true")
                    }
                    function fdisagree(){
                    window.location.href="https://www.google.com/"
                    return false;
                    }
                    </script>
                    </body>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 22 at 13:04

























                    answered Nov 22 at 12:57









                    MrAleister

                    429210




                    429210












                    • ok thx so if i understood that right make the <button> outside a form?
                      – Gard
                      Nov 22 at 15:06










                    • Oh nvm i fixed it by making it a button
                      – Gard
                      Nov 22 at 15:08


















                    • ok thx so if i understood that right make the <button> outside a form?
                      – Gard
                      Nov 22 at 15:06










                    • Oh nvm i fixed it by making it a button
                      – Gard
                      Nov 22 at 15:08
















                    ok thx so if i understood that right make the <button> outside a form?
                    – Gard
                    Nov 22 at 15:06




                    ok thx so if i understood that right make the <button> outside a form?
                    – Gard
                    Nov 22 at 15:06












                    Oh nvm i fixed it by making it a button
                    – Gard
                    Nov 22 at 15:08




                    Oh nvm i fixed it by making it a button
                    – Gard
                    Nov 22 at 15:08


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431513%2fjavascript-window-location-href-code-not-working%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)