MailTo From Javascript

前端 未结 2 1810
感情败类
感情败类 2021-02-19 08:28

I\'ve got a link button which is used to build up a mailto from the contents of the page. What is the best way to launch it from javascript without opening a blank window or dis

相关标签:
2条回答
  • 2021-02-19 09:10

    If you're using jQuery, then you can easily create an element based on the markup you build (refer to this answer for an example) and then invoke the link's click event by simply doing this:

    element.click();
    
    0 讨论(0)
  • 2021-02-19 09:21

    I don't think it's stupid at all! This is a good start. What I'd try next is just to create an actual link object in jquery, append it to the body, and then click() it.

    //window.location.href = sMailTo;
    $('<a href="' + sMailTo + '">click</a>').appendTo('body').click().remove();
    

    Should work as long as the escape() function you're using gets rid of all of the quotes and correctly encodes the html.

    But I have to say, this could be hard to get just right. If it still doesn't work, I'd recommend doing it server-side where the link POSTS all of the html needed.

    0 讨论(0)
提交回复
热议问题