简体   繁体   English

如何启动 eMail 客户端,然后使用 Javascript 进行页面重定向?

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

I'm required to make a website function exactly the same on other browsers as it does in IE6.我需要使网站 function 在其他浏览器上与在 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.在 IE 中,这会导致邮件客户端打开并准备好指定的消息,然后将浏览器重定向到 newPage.html。 Other browsers, however, only redirect to newPage.html.然而,其他浏览器仅重定向到 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.请注意,我也曾尝试使用元刷新来完成此操作,但没有成功。

Try using something like: 尝试使用类似的东西:

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

Instead of at the onload. 而不是onload。

Changing the href property will start a location load, changing it again afterwards will cancel the previous navigation. 更改href属性将启动位置加载,然后再次更改它将取消上一个导航。

It appears that IE6 will start the e-mail client immediately upon setting the property, then continue the javascript execution. 看来IE6会在设置属性后立即启动电子邮件客户端,然后继续执行javascript。 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: 我设法使用计时器在Chrome中解决此问题,它也适用于其他浏览器:

function myFunc(){ 
  location.href="mailto:test@test.com&body=Hello!"; 
  window.setTimeout(function () { location.href="newPage.html" }, 0); 
} 

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. 总的来说,我倾向于认为安全设置会妨碍你,并建议只给用户一个无聊的老式mailto链接点击。 (Edit: Perhaps one set up like Mic suggests.) (编辑:也许就像Mic建议的一样。)

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);
}

This will work only if the client's browser knows which E-Mail client to open for mailto: links in the first place. 这只有在客户端的浏览器知道首先为mailto: link打开哪个电子邮件客户端时才有效。 If the user uses a web-based client that is not registered with the browser, nothing will happen. 如果用户使用未在浏览器中注册的基于Web的客户端,则不会发生任何事情。

Also, it could be that security settings prevent mailto: links from opening programmatically, or will prevent it in the future. 此外,可能是安全设置阻止mailto:链接以编程方式打开,或将来阻止它。

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);
function redirect() {
    setTimeout(function() {
        location.href = "index.html";
    }, 1000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM