0

I read this post before and I need to click a link which is auto generated inside a AJAX response handler.

The code would like this,I use this html to auto start a mobile phone app.

<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>

However, the code above does not work. Is there a way to accomplish it?

Community
  • 1
  • 1
alwaysday1
  • 1,683
  • 5
  • 20
  • 36
  • 1
    Have you tried debugging it at all? Finding out specifically what isn't working? I use `debugger` or `console.log` to narrow down exactly what doesn't work. If the code gets all the way to `link.dispatchEvent(event);` and still doesn't seem to be triggering a click, then that's something to need help troubleshooting – Tom Prats Feb 02 '15 at 09:22
  • @TMP I've tested both the function `clickLink` and auto generate a link separately, and it both works. When I combined them, failed:( – alwaysday1 Feb 02 '15 at 09:39
  • 1
    But what part of it fails? Does the dispatchEvent or fireEvent functions not work? Or does it not reach them? – Tom Prats Feb 02 '15 at 09:43
  • 1
    @TMP I spent some time to debug it as you suggested. Now I know the answer. It seems that browser does not support NON-HTTP Protocol link auto click. When I changed `wx://` to `http://` it works. – alwaysday1 Feb 03 '15 at 03:13

1 Answers1

1

It seems that browser does not support NON-HTTP Protocol link to be auto clicked. When I changed wx:// to http:// it works.

alwaysday1
  • 1,683
  • 5
  • 20
  • 36