How can I launch the eMail client, and then do a page redirect with Javascript?
Asked Answered
P

5

4

I'm required to make a website function exactly the same on other browsers as it does in IE6. Part of the current code looks similar to this:

<script>
function myFunc(){
 location.href="mailto:[email protected]&body=Hello!";
 location.href="newPage.html";
}
</script>
<body onload="myFunc();">
</body>

in IE, this causes the mail client to open with the specified message prepared, and then redirects the browser to newPage.html. Other browsers, however, only redirect to newPage.html. How can I achieve this effect (opening the mail client and then doing a page redirect) consistently across browsers?
As a note, I've also tried to accomplish this using meta refresh, but was unsuccessful.

Prima answered 3/2, 2010 at 15:2 Comment(5)
Have you tried changing the <script> tag to <script type="text/javascript"> to see if there is a difference?Fiester
What other browsers? it works in my FF 3.5.7.Coadjutress
yeah, no luck. Thanks for trying, though.Prima
I've been testing in Chrome. It doesn't seem to be working in Firefox 3.5.1 for me on this machine, though, either.Prima
I edited my answer for a possible workaround, check it out.Snuck
P
6

Changing the href property will start a location load, changing it again afterwards will cancel the previous navigation.

It appears that IE6 will start the e-mail client immediately upon setting the property, then continue the javascript execution. Other browsers appear to do things differently, and the second location load will cancel the first.

I managed to work around this in Chrome with a timer, it might work for other browsers too:

function myFunc(){ 
  location.href="mailto:[email protected]&body=Hello!"; 
  window.setTimeout(function () { location.href="newPage.html" }, 0); 
} 
Peridot answered 3/2, 2010 at 15:15 Comment(5)
Yeesh, and he gives it to you instead of me. The bum. ;-) 32 seconds! Eons!Nedranedrah
Stop whining, I beat you both by 4 minutes! :) On the other hand, @Andy E you took the time to actually test it, so the mark went to the right person.Snuck
@T.R.: And you chose the right one. @Pekka: Oh, I agree (about Andy), I'm just joking around. :-) (But for the record: I'm pretty sure I posted my answer before you edited yours.)Nedranedrah
@T.J.Crowder: Right, I edited mine a few minutes later, you're right.Snuck
@Pekka: You beat my answer with your edit, so it must have been really close. I have questions sorted in order of oldest so that I mark the first correct one but I supposed that doesn't always work with edits. @T.J. Crowder: I knew you were joking ;-)Peridot
N
7

Try using something like:

<a href="mailto:[email protected]" onclick="window.location.href='np.html'">send</a>

Instead of at the onload.

Ngocnguyen answered 3/2, 2010 at 15:14 Comment(4)
On now that's smart. But I'd include a slight delay before changing the window location in the onclick handler.Nedranedrah
Generally speaking, I actually much prefer this solution, but in this specific case I think it's better for me to stick with the javascript based solution.Prima
Nice solution, indeed. But please don't forget the final " after the second url. ;)Samal
@Marcel I added the missing "Ngocnguyen
P
6

Changing the href property will start a location load, changing it again afterwards will cancel the previous navigation.

It appears that IE6 will start the e-mail client immediately upon setting the property, then continue the javascript execution. Other browsers appear to do things differently, and the second location load will cancel the first.

I managed to work around this in Chrome with a timer, it might work for other browsers too:

function myFunc(){ 
  location.href="mailto:[email protected]&body=Hello!"; 
  window.setTimeout(function () { location.href="newPage.html" }, 0); 
} 
Peridot answered 3/2, 2010 at 15:15 Comment(5)
Yeesh, and he gives it to you instead of me. The bum. ;-) 32 seconds! Eons!Nedranedrah
Stop whining, I beat you both by 4 minutes! :) On the other hand, @Andy E you took the time to actually test it, so the mark went to the right person.Snuck
@T.R.: And you chose the right one. @Pekka: Oh, I agree (about Andy), I'm just joking around. :-) (But for the record: I'm pretty sure I posted my answer before you edited yours.)Nedranedrah
@T.J.Crowder: Right, I edited mine a few minutes later, you're right.Snuck
@Pekka: You beat my answer with your edit, so it must have been really close. I have questions sorted in order of oldest so that I mark the first correct one but I supposed that doesn't always work with edits. @T.J. Crowder: I knew you were joking ;-)Peridot
N
4

On the whole, I tend to think security settings will get in your way and would recommend just giving the user a boring old-fashioned mailto link to click. (Edit: Perhaps one set up like Mic suggests.)

That said, I wonder if things become any more reliable if you introduce a delay:

function myFunc() {
    location.href = "mailto:[email protected]&body=Hello!";
    setTimeout(function() {
        location.href = "newPage.html";
    }, 500);
}
Nedranedrah answered 3/2, 2010 at 15:15 Comment(0)
S
1

This will work only if the client's browser knows which E-Mail client to open for mailto: links in the first place. If the user uses a web-based client that is not registered with the browser, nothing will happen.

Also, it could be that security settings prevent mailto: links from opening programmatically, or will prevent it in the future.

I wouldn't rely on this to work either way, only as a nice optional convenience function.

Anyway, to answer your question, can you try setting a timeout between the two calls? Maybe the location refresh is just too quick for the browser to catch up.

location.href="mailto:[email protected]&body=Hello!";
setTimeout(function(){ location.href = 'newPage.html' },  500);
Snuck answered 3/2, 2010 at 15:11 Comment(2)
lol you beat me while I was testing my answer. I found that a timeout interval of 0 works fine at least in Chrome. I'm also out of votes or I would +1 :-)Peridot
LOL! That's three of us now, my friend. :-) But I still like Mic's answer.Nedranedrah
M
0
function redirect() {
    setTimeout(function() {
        location.href = "index.html";
    }, 1000);
}
Macedoine answered 3/10, 2021 at 23:13 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂Lafave

© 2022 - 2024 — McMap. All rights reserved.