Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

javascript - Second `window.location = mailto:` fails as long as first one is still open

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...