Second `[removed] = mailto:` fails as long as firs

2019-09-19 13:04发布

I'm trying to open the local mail window using the javascript window.location.href=mailto:<addresses>. However, my addresses exceed the maximum length. So I slice it into pieces, and send these one after the other, after a specific timeout. However, the second relocation will not open a new (Outlook) mail window if the first is still open... Is there any way to get around this? Or is there another/better way to open multiple mail windows on the local client?

Any help would be greatly appreciated!

The code:

function Send_Mails(mails) {

var timeout = 2000;
var maxUrlCharacters = 1900;
var currIndex = 0;
var nextIndex = 0;

if (mails.length < maxUrlCharacters) {
    window.location = 'mailto:' + mails;
    return;
}

do {
    currIndex = nextIndex;
    nextIndex = mails.indexOf(';', currIndex + 1);
} while (nextIndex != -1 && nextIndex < maxUrlCharacters)

if (currIndex == -1) {
    window.location = 'mailto:' + mails;
} else {
    window.location = 'mailto:' + mails.slice(0, currIndex);
    setTimeout(function () {
                Send_Mails(mails.slice(currIndex + 1));
                }, timeout);
}

}

This opens the first mailwindow correctly, but the second one is never opened as long as the first one is open.

Best regards, Hans

1条回答
狗以群分
2楼-- · 2019-09-19 13:11

The sample script below works for me on localhost

<button onclick="openmail()">Open mail</button>
<script>
    function openmail(){
        window.location.href="mailto:test1@test.org"
        setTimeout(function(){
            console.log('2nd email');
            window.location.href="mailto:test2@test.org"
        }, 3000);
    }
</script>

When on Fiddle, it seems to be working 75% of the time (with ad blocker turned on).

There is a risk that popup and ad blockers, anti-virus software etc. may silently block forced opening of multiple mailto links.

查看更多
登录 后发表回答