window.location 在 javascript 中不起作用
window.location Does not work in javascript
某种原因,下面的代码在尝试重定向时不起作用。我已经尝试了其他几种方法,例如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");
}
}
- 为什么这个 .push 不起作用?(JavaScript)
- document.createelement不起作用(javascript)
- Chrome 中表格的 CSV 导出不起作用 - JavaScript/AngularJS
- 如果语句不起作用 - JavaScript
- 为什么我的动态更改链接不起作用?(Javascript)
- RPS 函数不起作用(JavaScript)
- 计时器间隔不起作用 Javascript
- 通过返回 false - 不起作用(javascript)来阻止链接打开
- Eval 不起作用 - JavaScript
- Android 浏览器 .click() 不起作用 javascript
- 为什么这段代码不起作用?Javascript
- jsFiddle显示正确,但一旦在本地运行就不起作用(Javascript)
- 带有html的Navigator不起作用javascript
- 从文本文件中提取内容不起作用 JavaScript
- 从查询字符串中获取参数;不起作用..Javascript
- regex手机验证不起作用-javascript
- if和else语句不起作用(javascript)(html)
- 更改字体大小的复选框不起作用-Javascript
- 为什么这个基本的 if 语句不起作用?JavaScript
- Timer 不起作用/ JavaScript/ innerHTML null