26

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/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?

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

JohnP
  • 49,507
  • 13
  • 108
  • 140
charlie_cat
  • 1,830
  • 7
  • 44
  • 73

6 Answers6

64

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()">
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    thank you! what is the diff between return true and return false?? – charlie_cat May 23 '11 at 07:44
  • 3
    @Helloise Smit: If you return false it will stop the default action of the event, i.e. the click on the button. Returning true (or nothing at all) will allow the click, which will submit the form. – Guffa May 23 '11 at 07:57
  • thank you..all is fine just one more thing please: i have a textarea with the submit where a user can type in a message and click on submit which will then call my window.location.href...how can i pass on the message to the new window? my form is a "post" and the id of textarea is "message" ? thank you – charlie_cat May 23 '11 at 08:21
  • @Helloise Smit: When you use `window.location.href` the browser always does a GET request. If you want that, you can add the value of the textarea to the URL in the same manner as `mailid`, using the `encodeURIComponent` function to encode the value first. If you want to make a POST request, then you would have to post the form instead of using `window.location.href`. – Guffa May 23 '11 at 10:19
  • thank you the window.location.href works perfect with mailid how would i then pass message also using encodeURIComponent and GET?? i have NO idea on how to do this please help i am very new to all this, thank you – charlie_cat May 23 '11 at 11:01
  • @Helloise Smit: This should work: `window.location.href = "http://www.rainbowcode.net/index.php/profiles/mail?="+mailid+"&message="+encodeURIComponent(this.form.message.value)`. Note that the amount of data that you can send in the URL is limited; in IE it's about 2 kB. If you post the form, it's only limited by how much the server accepts, usually something like 4 MB. – Guffa May 23 '11 at 13:42
  • sorry to do this here but i cannot post a new question of any kind not even to support..i get an error that says i do not meet the quality standards????? i dont understand can some one help please??? – charlie_cat May 24 '11 at 08:02
  • Thanks Guffa, It helped me, it saved my time, now I understood the issue with reloading the current page. – Lavanya Jan 12 '15 at 18:41
4

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;
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
alexxmed
  • 81
  • 1
  • Thank you so much , I don't have words to appreciate you. Just awesome. I was trying to resolve this issue from last 3 days. and suddenly got this. Thanks – Laxminarayan Charan Feb 21 '22 at 16:40
3

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.

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
  • thank you window.open works ,it opens a new window but i want to be able to click on the back arrow and return to the previous page please – charlie_cat May 23 '11 at 07:38
1

Try using replace function instead

window.location.replace('http://www.rainbowcode.net/index.php/profiles/mail?='+mailid)
a1626
  • 2,953
  • 1
  • 19
  • 34
  • You should show how exactly you used it so that it worked. The answer should answer the question asked, otherwise it's just a comment. Best regards – YakovL Apr 04 '18 at 08:59
0

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.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
0

Make sure you use a lowercase L: window.location, not window.Location

Oops.

Greg Gum
  • 33,478
  • 39
  • 162
  • 233