window.location 在 javascript 中不起作用

window.location Does not work in javascript

本文关键字:不起作用 javascript location window      更新时间:2023-09-26
由于

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

将 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="">

更改位置时,必须为其指定一个绝对 URL:

location.href = '/some_page_on_my_site';

或:

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

或:

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

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

> 2018

多浏览器:Safari,Chrome,Firefox:

只是这项工作:

window.location.replace(url)

尝试将 URL 分配给窗口的另一个选项。位置或窗口。位置失败

当你运行这个:

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://。

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

你忘了分配 myQuiz 变量

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

此外,您还需要在URL的开头添加http://。因为,否则它的意思是相对网址。

    <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>

问题应该在于调用一个没有相应对象的方法..将javascript更改为如下所示...getElementById("myQuiz"(是HTML文档的一种方法,在javascript中 object.so,要使用此方法,您必须将其附加到要对其进行操作的对象。请记住,getElementById(( 不是一个 JavaScript 方法。所以当你做document.getElementById("myQuiz"(时,你是在告诉javascript去文档对象(即HTML(运行方法getElementById("myQuiz"(。此方法将元素返回给 JS。从 DOM 中,所有 HTML 元素都是对象。因此,我们有一个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");
        }
     }