开发者

Why do mailto links in Chrome conflict with POST requests?

开发者 https://www.devze.com 2023-03-03 01:34 出处:网络
I\'m not entirely sure the question is correct but here\'s the situation. I have a webpage with two POST requests, which are open for some time (the response isn\'t suppose to come right away) while I

I'm not entirely sure the question is correct but here's the situation. I have a webpage with two POST requests, which are open for some time (the response isn't suppose to come right away) while I can be doing other things on the page. I also have a mailto link on th开发者_如何学Goe page. For some reason in Chrome, when I click that link, the two requests immediately return an error. I also noticed that the console in Chrome shows the mailto link as a GET request event (when it's clicked). What is going on here? Even if Chrome treats mailto links as requests, why should it conflict with any other requests on the page?

In Firefox the mailto link has zero affect on the requests, they just keep working and waiting on server response. Also, the link itself doesn't seem to be a request of any sort. BTW, the mailto opens up an Outlook message window (and that part works fine in Chrome, just the requests fail).

Also just in case, I'm using jQuery $.ajax to initiate the requests.

It was pointed out that perhaps Chrome treats a mailto link like a regular, at least in part, and so has some of the side effects. So then the question becomes how do I combine a mailto link with request on the page? I can't replace the link with a form.


I ran into this issue recently. This actually happens with mailto: or any other app URI in Chrome. The solution I used was to load the URL in an iframe:

$('body').append('<iframe id="mailto-launcher"></iframe>');
$('#mailto-launcher').get(0).contentWindow.location.href = 'mailto:?subject=test';

You can also style that iframe so that is it rendered off the page (using absolute positioning, etc). This will launch the mail client and still keep your document's AJAX requests alive.


Google Analytics support suggests to capture link clicks, send request to GA, wait 100ms and then update the URL. 100ms should be sufficient for tracking request to complete. https://support.google.com/analytics/answer/1136920?hl=en

The example is clunky, following is a modified version that handles all links automatically (also those created with javascript/loaded with AJAX):

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
  function trackOutboundLink(event){
    var url = event.currentTarget.href;
    try {
      _gaq.push(['_trackEvent', 'Outbound Links' , url]);
    } catch(err){}

    setTimeout(function() {
      document.location.href = url;
    }, 100);

    return false;
  }

  $(document).on('click', 'a[href]', trackOutboundLink);
</script>


Not sure, but it's possible that Chrome treats clicking mailto links as navigating to a new page.

Has all the side-effects of it, but none of the real effects (like reloading the page.)


This issue prevented me from sending event to Google Analytics when email link was clicked.

As a workaround I changed target of mailto: links to _blank. This causes the browser to open an empty tab before opening Mail.app, but GA integration works now.


Same here, POSTs to mixpanel were cancelled. Our workaround was to invoke the mailto: link using a setTimeout of 1 second, it works just fine. Not nice, tough, but at 10pm on Friday at the office it sounded kind of allright :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号