Javascript window.location() 不起作用,无法打开页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6094130/
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() not working, not opening page
提问by charlie_cat
I have a submit button with a onClick:
我有一个带有 onClick 的提交按钮:
<div id="messageDiv">
<form>
<textarea name="message" rows="10" cols="20"></textarea></textarea>
<br /><br />
<input type="submit" value="Send" onClick="sendmail()">
<input type="reset" value="Reset" name='reset'>
</form>
</div>
then I have my sendmail:
然后我有我的sendmail:
function sendmail()
{
window.location.href = "http://www.rainbowcode.net/index.php/profiles/mail?="+mailid;
window.location('http://www.rainbowcode.net/index.php/profiles/mail?='+mailid);
//return true;
}
mailid
is a global variable that gets set in another JS function and it does contain the correct value. How come window.location
is not opening my page?
mailid
是在另一个 JS 函数中设置的全局变量,它确实包含正确的值。怎么会window.location
不打开我的网页?
If I manually open it with a mailid it works fine..
如果我用mailid手动打开它,它工作正常..
回答by Guffa
Setting the location works just fine, but then the form is submitted, which will reload the current page instead.
设置位置工作正常,但随后提交表单,这将重新加载当前页面。
Return false from the method:
从方法返回 false:
function sendmail() {
window.location.href = "http://www.rainbowcode.net/index.php/profiles/mail?="+mailid;
return false;
}
and return that status in the event to stop the submit:
并在事件中返回该状态以停止提交:
<input type="submit" value="Send" onclick="return sendmail()">
回答by Thor Jacobsen
If you need to open a new window, you should use the window.open()method. window.locationrefers to the current windows address, and will only - when using window.location.reload()
- reload the CURRENT window.
如果你需要打开一个新窗口,你应该使用window.open()方法。window.location指的是当前窗口地址,并且只会 - 在使用时window.location.reload()
- 重新加载当前窗口。
回答by alexxmed
I spent 2 days trying every solution shown here and elsewhere, to no avail. Then I removed the form
tags, which served no purpose since there was no submit
button, and the problem went away using:
我花了 2 天时间尝试此处和其他地方显示的所有解决方案,但无济于事。然后我删除了form
标签,因为没有submit
按钮,所以没有用,问题消失了:
window.location = 'mypage.php', true;
回答by Amar pratap singh
Try using replace
function instead
尝试改用replace
函数
window.location.replace('http://www.rainbowcode.net/index.php/profiles/mail?='+mailid)
回答by DaddyMonster
Solid answers already but why fight the system? Particularly if you've called with jquery or onClick
- there might not be an inline return is my point - so instead you could just change the action on the submit
:
已经有了可靠的答案,但为什么要与系统作斗争呢?特别是如果您使用 jquery 调用或onClick
- 我的观点可能没有内联返回 - 因此您可以更改以下操作submit
:
document.form.action = 'http://www.rainbowcode.net/index.php'
document.form.action = 'http://www.rainbowcode.net/index.php'
then you can pick any of the form variables if you need them or ignore if not.
那么您可以根据需要选择任何表单变量,否则可以忽略。