简体   繁体   中英

mailto: URL not working in jQuery bound event handler

I have an icon that when clicked will fire off a mailto: URL. Seems simple enough, but it's not working. When debugging I click the icon and the event handler does fire and executes the code within it, but then does nothing. Why doesn't this work?

<i id="email-icon" class="fa fa-envelope"></i>
$("#email-icon").on("click", function () {
    window.location.href = "mailto:mail@example.org";
});

I have a feeling the event is getting canceled in jQuery somehow?

尝试这个:

<a href="mailto:mail@example.org"><i id="email-icon" class="fa fa-envelope"></i></a>

So, to answer my own question....

There was actually nothing wrong with my code. The problem lied in how my new machine was configured!

In looking at my default programs in Control Panel, I saw that Chrome was set as the default program to handle MAILTO protocol. :-(

I made Outlook the handler for that protocol and then it worked as expected. Not sure how that happened, but thought I'd post it in case someone else has this issue down the road.

Thank you to Rory McCrossan for investigating.

try it to create direct link:

$("#email-icon").on("click", function () {
    var link = "mailto:mail@example.org";
    var obj = $("<a href=\"" + link + "\" target=\"_new\"><span></span></a>");
    obj.appendTo("body");
    obj.children("span:eq(0)").trigger("click");
    obj.remove();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM