window.location 在 javascript 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10646323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.location Does not work in javascript
提问by George Kashouh
This code below for some reason doesn't work when trying to redirect. I have tried several other ways such as window.location.href and more. The only thing that works is window.open ("")
but this opens a page in a new window when I need it to be on the same. If I do window.open("", "_self")
then it does not work again. If I replace the window.location
with an alert
it works fine, so I think the overall code is normal just something preventing it from redirecting on the same page. This issue is also on my Windows Chrome and a Mac Firefox.
尝试重定向时,以下代码由于某种原因不起作用。我尝试了其他几种方法,例如 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>
回答by artwl
change js to:
将 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;
};
and change:
并改变:
<form id="myQuiz" onsubmit="checkAnswers()" action="">
to
到
<form id="myQuiz" onsubmit="return checkAnswers();" action="">
回答by Ja?ck
When you change the location, you must give it an absolute URL:
当你改变位置时,你必须给它一个绝对的 URL:
location.href = '/some_page_on_my_site';
Or:
或者:
location.href = 'http://www.google.com';
Or:
或者:
location.href = '//www.google.com';
The last one will go to http or https, depending the current scheme. Thanks @Derek
最后一个将转到 http 或 https,具体取决于当前方案。谢谢@德里克
回答by jmort253
When you run this:
当你运行这个:
window.location ="www.google.com";
You're running this relative to the current requestURI. Thus, if you are on the http://example.compage, then you're attempting to redirect to:
您正在相对于当前的 requestURI 运行它。因此,如果您在http://example.com页面上,那么您正在尝试重定向到:
http://example.com/www.google.com
Instead, just remember to include the protocol:
相反,请记住包含协议:
window.location ="http://www.google.com";
Remember, window.location is not your address bar, designed to deal with nontechnical people entering a web address in an address bar. With window.location, you must explicitly include http://.
请记住,window.location 不是您的地址栏,它旨在处理非技术人员在地址栏中输入网址的问题。对于 window.location,您必须明确包含 http://。
回答by stackdave
2018
2018年
multibrowser: safari, chrome, firefox:
多浏览器:safari、chrome、firefox:
just this work:
只是这项工作:
window.location.replace(url)
another options that tries assign url to window.location or window.location fails
尝试将 url 分配给 window.location 或 window.location 的另一个选项失败
回答by Larry Cinnabar
You forgot to assign myQuiz variable
您忘记分配 myQuiz 变量
var myQuiz = document.getElementById( "myQuiz" );
also you need to add http://
in the beginning of your url. Because, otherwise it is meant as relative url.
您还需要http://
在 url 的开头添加。因为,否则它意味着相对 url。
回答by Daedalus
Your url used in window.location
is local; you need to add in the http://
part of the url.
您使用的网址window.location
是本地的;你需要添加在http://
url的部分。
回答by becky
<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>
回答by FLoat
the problem should be with calling a method without the respective object.. change the javascript to be as shown below... getElementById("myQuiz") is a method of the HTML document object.so when in javascript, to use this method you have to attach it to object with which its to act on. Remember getElementById() is not a javascript method. so when you do document.getElementById("myQuiz") you are tellin the javascript to go the document object(i.e HTML) run the method getElementById("myQuiz"). This method returns to JS an element. From DOM, all HTML elements are Objects.. So, we have an Obj_Form which is the element Object from HTML..
问题应该是在没有相应对象的情况下调用方法.. 将 javascript 更改为如下所示... getElementById("myQuiz") 是 HTML 文档对象的方法。所以在 javascript 中,要使用此方法,您必须将其附加到要对其进行操作的对象上。记住 getElementById() 不是一个 javascript 方法。因此,当您执行 document.getElementById("myQuiz") 时,您会告诉 javascript 去文档对象(即 HTML)运行 getElementById("myQuiz") 方法。这个方法返回给JS一个元素。在 DOM 中,所有 HTML 元素都是对象。所以,我们有一个 Obj_Form,它是来自 HTML 的元素对象。
function checkAnswers(){
var Obj_Form=document.getElementById("myQuiz");
if(Obj_Form.elements[1].checked){
window.location = "http://www.google.com";
}
else{ alert("Failed");
}
}