Window.location Doesn't work in javascript

This code below for some reason does not work when trying to redirect. I tried several other ways such as window.location.href and more. The only thing that works is this window.open (""), but it opens the page in a new window when I need it to be on the same one. If I do window.open("", "_self"), then it won’t work. If I replaced window.locationwith alert, it works fine, so I believe that the general code is normal, something prevents it from redirecting to the same page. This issue also applies to my Windows Chrome and Mac Firefox.

<html>
    <head>
        <script type="text/javascript">
            function checkAnswers(){    
                getElementById("myQuiz");
                if(myQuiz.elements[1].checked){
                    window.location = "www.google.com";
                }else{
                    alert("Failed");
                }
            };
        </script>
    </head>
    <body>
        <img src="Castle.JPG" />
        <form id="myQuiz" onsubmit="checkAnswers()" action="">
            <p>Name the country this castle is located in?
                <br/>
                <input type="radio" name="radiobutton" value="A"/>
                <label>England</label>
                <input type="radio" name="radiobutton" value="B"/>
                <label>Ireland</label>
                <input type="radio" name="radiobutton" value="C"/>
                <label>Spain</label>
                <input type="radio" name="radiobutton" value="D"/>
                <label>France</label>
                <input type="submit" name="submit" value="Submit"/>
                <input type="reset" name="reset" value="Reset"/>
            </p>
        </form>
    </body>
</html>
+5
source share
7 answers

change js to:

function checkAnswers()
{
    var myQuiz=document.getElementById("myQuiz");
    if (myQuiz.elements[1].checked) {
        window.location.href = "http://www.google.com";
    }
    else {
        alert("Failed");
    }
    return false;
};

and change:

<form id="myQuiz" onsubmit="checkAnswers()" action="">

to

<form id="myQuiz" onsubmit="return checkAnswers();" action="">
+10

URL-:

location.href = '/some_page_on_my_site';

:

location.href = 'http://www.google.com';

:

location.href = '//www.google.com';

http https, . @Derek

+3

:

window.location ="www.google.com";

URI. , http://example.com, :

http://example.com/www.google.com

:

window.location ="http://www.google.com";

, window.location , , - . window.location http://.

+2

URL-, window.location, ; http:// URL-.

+1

myQuiz

var myQuiz = document.getElementById( "myQuiz" );

http:// URL-. URL.

+1
    <script type="text/javascript">
        function checkAnswers(){   

            var myQuiz = document.getElementById( "myQuiz" );
            if (myQuiz.elements[1].checked){ alert("123");
                window.location = "www.google.com";
            }else{
                alert("Failed");
            }
        };
    </script>
  </head>
<body>
    <img src="Castle.JPG" />
    <form id="myQuiz" onsubmit="return checkAnswers()" action="">
        <p>Name the country this castle is located in?
            <br/>
            <input type="radio" name="radiobutton" value="A"/>
            <label>England</label>
            <input type="radio" name="radiobutton" value="B"/>
            <label>Ireland</label>
            <input type="radio" name="radiobutton" value="C"/>
            <label>Spain</label>
            <input type="radio" name="radiobutton" value="D"/>
            <label>France</label>
            <input type="submit" name="submit" value="Submit"/>
            <input type="reset" name="reset" value="Reset"/>
        </p>
    </form>
</body>
-1

. javascript , ... getElementById ( "myQuiz" ) - HTML object.so, javascript , . , getElementById() javascript-. , document.getElementById( "myQuiz" ), , javascript (, HTML) getElementById ( "myQuiz" ). JS . DOM HTML - . , Obj_Form, Object from HTML.

function checkAnswers(){    
     var Obj_Form=document.getElementById("myQuiz");
      if(Obj_Form.elements[1].checked){
          window.location = "http://www.google.com";
          }
      else{ alert("Failed");
        }
     }
-1

All Articles