mailto using javascript
Asked Answered
N

7

74

I want to open a new outlook mail template with the 'To address' whenever a user clicks an image. I have return my code in a html page(linked with the image), whenever it loads the javascript should open a new mail template. But the functionality is not working. Kindly let me know what is wrong in my code.

body onLoad="redirect()"

script language="JavaScript"

function redirect() 

      var email = "[email protected]"
      var mailto_link = 'mailto:' + email
      window = window.open(mailto_link, 'emailWindow')
      if (window && window.open && !window.closed)         
          window.close()
Nilson answered 16/4, 2012 at 10:34 Comment(2)
window is a read-only variable.Varro
Is it because of a popup blocker?Storage
A
100

No need for jQuery. And it isn't necessary to open a new window. Protocols which doesn't return HTTP data to the browser (mailto:, irc://, magnet:, ftp:// (<- it depends how it is implemented, normally the browser has an FTP client built in)) can be queried in the same window without losing the current content. In your case:

function redirect()
{
    window.location.href = "mailto:[email protected]";
}
<body onload="javascript: redirect();">

Or just directly

<body onload="javascript: window.location.href='mailto:[email protected]';">
Archery answered 18/7, 2013 at 12:25 Comment(3)
It should be noted, that the browser will still trigger the onbeforeunload-Event: window.onbeforeunload = () => {console.log("onbeforeunload")}; window.location.href = "mailto:[email protected]";Bowne
@DB Thanks for pointing this interesting detail out! This makes sense... I guess, because, like I previously said, the browser might have a build-in tool for handling pseudo-protocols, like ftp://. This would indeed replace the current page contents, so the onbeforeunload event is fired, even if there is no built-in tool in the browser to handle mailto:// URIs. But this is just a personal assumption. In this case, the browser vendors should check first, if they support the pseudo-protocol, otherwise not firing the onbeforeunload event.Archery
@cjm2671 I tested the above code in the current newest Chrome version 76.0.3809.100 in normal mode and incognito mode. Everything works fine. Do you have any browser extensions (AddOns), which might prevent the code execution?Archery
S
27

Please find the code in jsFiddle. It uses jQuery to modify the href of the link. You can use any other library in its place. It should work.

HTML

<a id="emailLnk" href="#">
    <img src="http://ssl.gstatic.com/gb/images/j_e6a6aca6.png">
</a>

JS

$(document).ready(function() {
    $("#emailLnk").attr('href',"mailto:[email protected]");
});​

UPDATE

Another code sample, if the id is known only during the click event

$(document).ready(function() {
    $("#emailLnk").click(function()
     {
         window.location.href = "mailto:[email protected]";
     });
});​
Storage answered 16/4, 2012 at 11:1 Comment(0)
N
27

With JavaScript you can create a link 'on the fly' using something like:

var mail = document.createElement("a");
mail.href = "mailto:[email protected]";
mail.click();

This is redirected by the browser to some mail client installed on the machine without losing the content of the current window ... and you would not need any API like 'jQuery'.

Nonaligned answered 29/3, 2018 at 15:24 Comment(1)
window.open('mailto') normally leaves a blank tab open with mailto in the URL so this way seems much nicerAthanor
S
5

You can use the simple mailto, see below for the simple markup.

<a href="mailto:[email protected]">Click here to mail</a>

Once clicked, it will open your Outlook or whatever email client you have set.

Sedimentary answered 30/1, 2014 at 3:10 Comment(1)
Hey Grant, thanks for editing my terminology to used. I'm in hurry too type.Sedimentary
R
4

I've simply used this javascript code (using jquery but it's not strictly necessary) :

    $( "#button" ).on( "click", function(event) {
         $(this).attr('href', 'mailto:[email protected]?subject=hello');
    });

When users click on the link, we replace the href attribute of the clicked element.

Be careful don't prevent the default comportment (event.preventDefault), we must let do it because we have just replaced the href where to go

I think robots can't see it, the address is protected from spams.

Rabat answered 6/12, 2013 at 16:47 Comment(0)
S
1
document.location.href = "mailto:[email protected]"
Swahili answered 10/10, 2021 at 20:21 Comment(1)
A code-only answer is not high quality. While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please edit your answer to include explanation and link to relevant documentation.Fit
P
-1

I don't know if it helps, but using jQuery, to hide an email address, I did :

    $(function() {
        // planque l'adresse mail
        var mailSplitted 
            = ['mai', 'to:mye', 'mail@', 'addre', 'ss.fr'];

        var link = mailSplitted.join('');
        link = '<a href="' + link + '"</a>';
        $('mytag').wrap(link);
    });

I hope it helps.

Posticous answered 24/4, 2020 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.