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

怎甘沉沦 提交于 2019-12-19 09:47:57

问题


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:test@test.com&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.


回答1:


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:test@test.com&body=Hello!"; 
  window.setTimeout(function () { location.href="newPage.html" }, 0); 
} 



回答2:


Try using something like:

<a href="mailto:mail@domain.com" onclick="window.location.href='np.html'">send</a>

Instead of at the onload.




回答3:


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:test@test.com&body=Hello!";
    setTimeout(function() {
        location.href = "newPage.html";
    }, 500);
}



回答4:


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:test@test.com&body=Hello!";
setTimeout(function(){ location.href = 'newPage.html' },  500);


来源:https://stackoverflow.com/questions/2192860/how-can-i-launch-the-email-client-and-then-do-a-page-redirect-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!