Invoke / click a mailto link with JQuery / JavaScript

前端 未结 4 573
遇见更好的自我
遇见更好的自我 2020-12-13 03:18

I\'d like to invoke a mailto link from JavaScript - that is I\'d like a method that allows me to open the email client on the users PC, exactly as if they had clicked on a n

相关标签:
4条回答
  • 2020-12-13 03:48

    the working answer for me, tested in chrome, IE and firefox together with outlook was this

    window.location.href = 'mailto:address@dmail.com?subject=Hello there&body=This is the body';
    

    %0d%0a is the new line symbol of the email body in a mailto link

    %20 is the space symbol that should be used, but it worked for me as well with normal space

    0 讨论(0)
  • 2020-12-13 03:50

    You can avoid the blank page issue discussed above by instead using .click() with a link on the page:

    document.getElementById('mymailto').click();
    ...
    <a href="mailto:...." id="mymailto" style="display:none"></a>
    
    0 讨论(0)
  • 2020-12-13 03:52

    You can use window.location.href here, like this:

    window.location.href = "mailto:address@dmail.com";
    
    0 讨论(0)
  • 2020-12-13 04:01

    Actually, there is a possibillity to avoid the empty page.

    I found out, you can simply insert an iframe with the mailto link into the dom. This works on current Firefox/Chrome and IE (also IE will display a short confirm dialog).

    Using jQuery, I got this:

    var initMailtoButton = function()
    {
        var iframe = $('<iframe id="mailtoFrame" src="mailto:name@domain.com" width="1" height="1" border="0" frameborder="0"></iframe>');
        var button = $('#mailtoMessageSend');    
        if (button.length > 0) {            
            button.click(function(){
                // create the iframe
                $('body').append(iframe);
                //remove the iframe, we don't need it any more
                window.setTimeout(function(){
                    iframe.remove();    
                }, 500);
    
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题