window.location在javascript中不起作用

时间:2012-05-18 03:26:20

标签: javascript function redirect window.location

由于某种原因,下面的代码在尝试重定向时不起作用。我尝试过其他几种方法,比如window.location.href等等。唯一有效的是window.open (""),但是当我需要它在同一个窗口时,这将在新窗口中打开一个页面。如果我window.open("", "_self")那么它再也不起作用了。如果我用window.location替换alert它可以正常工作,所以我认为整体代码是正常的,只是阻止它在同一页面上重定向的东西。此问题也出现在我的Windows Chrome和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>

8 个答案:

答案 0 :(得分:13)

将js更改为:

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

并改变:

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

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

答案 1 :(得分:3)

更改位置时,必须为其指定绝对网址:

location.href = '/some_page_on_my_site';

或者:

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

或者:

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

最后一个将转到http或https,具体取决于当前的方案。谢谢@Derek

答案 2 :(得分:2)

运行时:

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

你是在相对于当前的requestURI运行它。因此,如果您在http://example.com页面上,那么您正尝试重定向到:

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

相反,只需记住包含协议:

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

请记住,window.location不是您的地址栏,旨在处理在地址栏中输入网址的非技术人员。使用window.location,您必须明确包含http://。

答案 3 :(得分:1)

window.location中使用的网址是本地的;您需要添加网址的http://部分。

答案 4 :(得分:1)

您忘记分配myQuiz变量

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

您还需要在网址的开头添加http://。因为,否则就是相对网址。

答案 5 :(得分:1)

2018

多浏览器:野生动物园,Chrome,Firefox:

仅此一项工作

window.location.replace(url)

另一个尝试将URL分配给window.location或window.location的选项失败

答案 6 :(得分:-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>

答案 7 :(得分:-1)

问题应该是调用没有相应对象的方法..将javascript更改为如下所示... getElementById(“myQuiz”)是HTML文档对象的一个​​方法。所以在javascript中,要使用您必须将此方法附加到要对其执行操作的对象。记住getElementById()不是一个javascript方法。因此,当你执行document.getElementById(“myQuiz”)时,你告诉javascript去文档对象(即HTML)运行方法getElementById(“myQuiz”)。此方法返回JS一个元素。从DOM开始,所有HTML元素都是Objects ..所以,我们有一个Obj_Form,它是来自HTML的元素Object ..

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