简体   繁体   English

window.location() 不起作用,无法打开页面

[英]window.location() not working, not opening page

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. mailid是在另一个 JS 函数中设置的全局变量,它确实包含正确的值。 How come window.location is not opening my page? window.location怎么window.location我的页面?

If I manually open it with a mailid it works fine..如果我用mailid手动打开它,它工作正常..

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()">

I spent 2 days trying every solution shown here and elsewhere, to no avail.我花了 2 天时间尝试此处和其他地方显示的所有解决方案,但无济于事。 Then I removed the form tags, which served no purpose since there was no submit button, and the problem went away using:然后我删除了form标签,因为没有submit按钮,所以没有用,问题消失了:

window.location = 'mypage.php', true;

If you need to open a new window, you should use the window.open() method.如果你需要打开一个新窗口,你应该使用window.open()方法。 window.location refers to the current windows address, and will only - when using window.location.reload() - reload the CURRENT window. window.location指的是当前窗口地址,并且只会 - 当使用window.location.reload() - 重新加载当前窗口。

尝试使用replace功能

window.location.replace('http://www.rainbowcode.net/index.php/profiles/mail?='+mailid)

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'

then you can pick any of the form variables if you need them or ignore if not.那么您可以根据需要选择任何表单变量,否则可以忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM