如何自动点击AJAX响应处理程序中自动生成的链接?

时间:2015-02-02 09:15:40

标签: javascript ajax

之前我读过这个post,我需要点击一个在AJAX响应处理程序中自动生成的链接。

代码就是这样,我使用这个html来自动启动手机应用程序。

<html>
<head>
<title>Click test</title>
</head>
<body>

<script>
function clickLink(link) {
    var cancelled = false;

    if (document.createEvent) {
        var event = document.createEvent("MouseEvents");
        event.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0,
            false, false, false, false,
            0, null);
        cancelled = !link.dispatchEvent(event);
    }
    else if (link.fireEvent) {
        cancelled = !link.fireEvent("onclick");
    }

    if (!cancelled) {
        window.location = link.href;
    }
}

function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]
 if (window.ActiveXObject){
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
   }
  }
 }
 else if (window.XMLHttpRequest)
  return new XMLHttpRequest()
 else
  return false
}

var mypostrequest=new ajaxRequest()

mypostrequest.onreadystatechange=function(){
 if (mypostrequest.readyState==4){
  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
   gid=mypostrequest.responseText
   url_intent = "mx://someprotol.tk/ow?gid=" + gid
   var a = document.createElement('a');
   var linkText = document.createTextNode("click me");
   a.appendChild(linkText);
   a.title = "click me";
   a.href = url_intent;
   document.body.appendChild(a);
   clickLink(a);
  }
  else{
   alert("An error has occured making the request")
  }
 }
}

var parameters="ua=g189"
mypostrequest.open("GET", "gid?"+parameters, true)
mypostrequest.send(parameters)
</script>
to be continue:)
</body>
</html>

但是,上面的代码不起作用。有没有办法实现它?

1 个答案:

答案 0 :(得分:1)

浏览器似乎不支持自动点击NON-HTTP协议链接。当我将wx://更改为http://时,它可以正常工作。