开发者

window.location() not working, not opening page

开发者 https://www.devze.com 2023-03-07 13:24 出处:网络
I have a submit button with a onClick: <div id=\"messageDiv\"> <form> <textarea name=\"message\" rows=\"10\" cols=\"20\"></textarea></textarea>

I have a submit button with a 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:

   function sendmail()
   {   
      window.location.href = "http://www.rainbowcode.net/index.php/pro开发者_StackOverflow社区files/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?

If I manually open it with a mailid it works fine..


Setting the location works just fine, but then the form is submitted, which will reload the current page instead.

Return false from the method:

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. Then I removed the form tags, which served no purpose since there was no submit button, and the problem went away using:

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


If you need to open a new window, you should use the window.open() method. window.location refers to the current windows address, and will only - when using window.location.reload() - reload the CURRENT window.


Try using replace function instead

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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消