I am installing href mailto from jQuery, but I do not know how to set the target _blank

I have a hyperlink on my web page:

<a id="contact" href="#">contact</a>

And then in jQuery I have:

$("#contact").click(function() {
    document.location.href = "mailto:info@mail.com";
});

When a user clicks on a link, it opens in the same window, how can I open mail in another window? I tried adding target="_blank"in <a.., but then the contact form opened in the same window and my web page opened in another window ... How can I fix it?

+4
source share
3 answers

Use window.openinsteaddocument.location.href

  $("#contact").click(function() {
    win = window.open('mailto:info@mail.com', '_blank');
    if (win) {
        //Browser has allowed it to be opened
        win.focus();
    } else {
        //Browser has blocked it
        alert('Please allow popups for this website');
    }
});
+3
source

You can use window.open as follows:

  var win = window.open(mailto:info@mail.com, '_blank');
  win.focus();
+1
source

, jquery / .

$("#contact").attr("href", "mailto:info@mail.com").attr("target", "_blank");

, ( ).

0
source

Source: https://habr.com/ru/post/1666257/


All Articles