Open the href mailto link in new tab / window

后端 未结 8 914
臣服心动
臣服心动 2020-12-05 09:56

I have an image which when click, I want to link to a mailto:

 
        

        
8条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 10:43

    I know this is an old question, but this thread had the best set of answers if found. I modified Marcos's Answer above to also close the blank tab that is created if the client has an external mail handler

    reference answer

    JS (w\ jQuery for event handlers)

    $(document).on('click', 'a[href^=mailto]', function(e) {
      var checkClose, checkLoaded, event, href, i, len, loadEvents, results, t, wndw;
      e.preventDefault();
      href = this.href;
      wndw = window.open(href, 'mail');
      checkClose = function() {
        console.log('checkClose');
        try {
          wndw.location.href;
          return wndw.close();
        } catch (error) {
          return console.log('webmail');
        }
      };
      t = setTimeout(checkClose, 5000);
      try {
        checkLoaded = function() {
          console.log('loaded');
          clearTimeout(t);
          return t = setTimeout(checkClose, 2000);
        };
        wndw.onload = checkLoaded;
        loadEvents = ["DomContentLoaded", "load", "beforeunload", "unload"];
        results = [];
        for (i = 0, len = loadEvents.length; i < len; i++) {
          event = loadEvents[i];
          results.push(wndw.addEventListener(event, checkLoaded));
        }
        return results;
      } catch (error) {
        return checkLoaded();
      }
    });
    

    jsfiddle

提交回复
热议问题