JavaEar 专注于收集分享传播有价值的技术资料

在Ajax回调中打开电子邮件客户端。不适用于iPhone (Open email client within Ajax callback. Not working on iPhone)

I need to open an email client via javascript. I need to do this within the success function of an ajax call.

My issue is that this does not work on the iPhone. It works fine when it's not within the Ajax call.

function open_email_client(){
    var a = document.createElement('a');
    a.setAttribute("href", "mailto:myfriend");
    a.setAttribute("target", "_blank");

    var dispatch = document.createEvent("HTMLEvents");
    dispatch.initEvent("click", true, true);
    a.dispatchEvent(dispatch); 
}

// open_email_client(); Works here for computer (Chrome) and iPhone (Safari)


$.ajax({
    type: "POST",
    url: "myurl/", 
    data: {},
    success: function(data){
       open_email_client(); // Only works on computer, not iPhone. Tested with alert() that it does go here.
    }
    ,error: function(e) {
    } 
});  

I've also tried:

 document.location.href = "mailto:my_friend";

This works fine for the iPhone (also within Ajax), but is no good on the computer because I need the client to open in a new window or tab.

I'd like a solution that does not require that I test which sort of device I'm on.

1个回答

    最佳答案

  1. 英文原文

    I ended up solving it a different way, which was possible because the initial action is a manual click.

    Note that the ajax calls has to have async=false.

    <a id = "email_link" target = "blank_"  >
        <div>Email</div>
    </a>
    
    
    $("#email_link").click(function(){ 
        $.ajax({
            type: "POST",
            url: "myurl/", 
            async: false,
            data: {},
            success: function(data){
              $("#email_link").attr("href","mailto:myfriend"); 
            }
            ,error: function(e) {     
            } 
        });  
    }); 
    

    中文翻译

    我最终以不同的方式解决了这个问题,这是可能的,因为最初的操作是手动点击。

    请注意,ajax调用必须具有async = false。

     &lt; a id =" email_link" target =" blank_" &gt;
        &LT; DIV&GT;电子邮件与LT; / DIV&GT;
    &LT; / A&GT;
    
    
    $( " #EMAIL_LINK" )。点击(函数(){
        $就({
            类型:" POST" ,
            url:" myurl /" ,
            async:false,
            数据:{},
            成功:函数(数据){
              $( " #EMAIL_LINK" )ATTR( " HREF" , " 电子邮件地址:myfriend" )。
            }
            ,错误:function(e){
            }
        });
    });
     

    I ended up solving it a different way, which was possible because the initial action is a manual click.

    Note that the ajax calls has to have async=false.

    <a id = "email_link" target = "blank_"  >
        <div>Email</div>
    </a>
    
    
    $("#email_link").click(function(){ 
        $.ajax({
            type: "POST",
            url: "myurl/", 
            async: false,
            data: {},
            success: function(data){
              $("#email_link").attr("href","mailto:myfriend"); 
            }
            ,error: function(e) {     
            } 
        });  
    }); 
    

    我最终以不同的方式解决了这个问题,这是可能的,因为最初的操作是手动点击。

    请注意,ajax调用必须具有async = false。

     &lt; a id =" email_link" target =" blank_" &gt;
        &LT; DIV&GT;电子邮件与LT; / DIV&GT;
    &LT; / A&GT;
    
    
    $( " #EMAIL_LINK" )。点击(函数(){
        $就({
            类型:" POST" ,
            url:" myurl /" ,
            async:false,
            数据:{},
            成功:函数(数据){
              $( " #EMAIL_LINK" )ATTR( " HREF" , " 电子邮件地址:myfriend" )。
            }
            ,错误:function(e){
            }
        });
    });