Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location() not working, not opening page

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..

like image 698
charlie_cat Avatar asked Sep 07 '25 16:09

charlie_cat


2 Answers

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()">
like image 106
Guffa Avatar answered Sep 09 '25 05:09

Guffa


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;
like image 41
alexxmed Avatar answered Sep 09 '25 05:09

alexxmed