2

I have following code (HTTPS AJAX) and in IE 11 this runs well only in debug mode. The problem is that in IE onclick event does not trigger any action. But how to debug it? When I open dev tools, script runs just fine and console.log is empty. I know i have many commented code out, but no console() is there. I removed this already thinking this is the problem. What can be a cause that IE 11 does not accept it?

UPDATE: I tried various things, META tags, cache control, etc, but nothing helped. Only what helped was to add Math.random() to control caching.

<meta http-equiv="X-UA-Compatible" content="IE=9"/> does not work

req.setRequestHeader('Cache-Control', 'no-cache'); does not work

Math.random() works

    <?php include "vars.php"; ?>


    <script type="text/javascript">

    function HandleGPIO(gpin,value) {
        document.getElementById(gpin).innerHTML="Working";
        document.getElementById(gpin).style.color="blue";
        var req = createHttpRequest();
        req.open("GET", "https://mysite.xy/gpio/gpioajax.php?gpio=" + gpin + "&value=" + value, false); //false ceka na vyrizeni
        req.setRequestHeader("Content-Type", "text/html; charset=windows-1250");
        req.send(null);
            if(req.responseText == 0){  
                QueryGPIO();    //Query for updated values
                return true;
                }
            else if(req.responseText == 1){
                QueryGPIO();    //Query for updated values
                return true;
                }
            else{
                window.alert('Spatny navratovy kod pro AJAX, chyba: ' + req.responseText);
                QueryGPIO();    //Query for updated values
                return false;
                }
    }

    function QueryGPIO() {
        //var arr_inputs = ['5','21','27'];
        var arr_inputs = <?php echo '["' . implode('", "', $pins_array_in) . '"]' ?>;
        var arr_outputs = <?php echo '["' . implode('", "', $pins_array_out) . '"]' ?>;

        //Query Inputs
        for (var i = 0; i < arr_inputs.length; i++) {
            (function(i) {
                //alert(arr_inputs[i]);
                var req = createHttpRequest();
                req.open("GET", "https://mysite.xy/gpio/gpioajaxquery.php?gpio=" + arr_inputs[i], false); //false ceka na vyrizeni
                req.setRequestHeader("Content-Type", "text/html; charset=windows-1250");
                req.send(null);
                //window.alert(req.responseText);
                if(req.responseText == '0'){
                    document.getElementById(arr_inputs[i]).innerHTML='ON';
                    document.getElementById(arr_inputs[i]).style.color='green';
                    //document.getElementById(arr_inputs[i]).addEventListener("click", function() { HandleGPIO(arr_inputs[i], 1); }, false);    
                    //document.getElementById(arr_inputs[i]).setAttribute('onclick','HandleGPIO('+arr_inputs[i]+',1)');
                    document.getElementById(arr_inputs[i]).onclick = function () { HandleGPIO(arr_inputs[i],1); };
                    }
                else if(req.responseText == '1'){
                    document.getElementById(arr_inputs[i]).innerHTML='OFF';
                    document.getElementById(arr_inputs[i]).style.color='red';
                    //document.getElementById(arr_inputs[i]).setAttribute('onclick','HandleGPIO('+arr_inputs[i]+',0)');
                    document.getElementById(arr_inputs[i]).onclick = function(debug) {
                    if(debug === true) {
                return [arr_inputs[i], 0];
              }
            else {
                HandleGPIO(arr_inputs[i],0);
              }
            }
                    //document.getElementById(arr_inputs[i]).onclick = function() { HandleGPIO(arr_inputs[i],0); };
                    var temp = document.getElementById(arr_inputs[i]).onclick;
                    }
                else{
                    window.alert('Spatny navratovy kod pro AJAX');
                    }
            })(i)
    }   
        //var arr_outputs = ['9','12'];

        //Query Outputs
        for (var i = 0; i < arr_outputs.length; i++) {
            (function(i) {      
                var req = createHttpRequest();
                req.open("GET", "https://mysite.xy/gpio/gpioajaxquery.php?gpio=" + arr_outputs[i] + "&r=" + Math.random(), false); //false ceka na vyrizeni
                req.setRequestHeader("Content-Type", "text/html; charset=windows-1250");
                req.send(null);
                //window.alert(req.responseText);
                if(req.responseText == '1'){
                    document.getElementById(arr_outputs[i]).innerHTML='ON';
                    document.getElementById(arr_outputs[i]).style.color='green';
                    //document.getElementById(arr_outputs[i]).setAttribute('onclick','HandleGPIO('+arr_outputs[i]+',0)');
                    //document.getElementById(arr_outputs[i]).addEventListener("click", function() { HandleGPIO(arr_outputs[i], 0); },true);
                    document.getElementById(arr_outputs[i]).onclick = function() { HandleGPIO(arr_outputs[i],0); };
                    }
                else if(req.responseText == '0'){
                    document.getElementById(arr_outputs[i]).innerHTML='OFF';
                    document.getElementById(arr_outputs[i]).style.color='red';
                    //document.getElementById(arr_outputs[i]).setAttribute('onclick','HandleGPIO('+arr_outputs[i]+',1)');
                    //document.getElementById(arr_outputs[i]).addEventListener("click", function() { HandleGPIO(arr_outputs[i], 1); },true);
                    document.getElementById(arr_outputs[i]).onclick = function() { HandleGPIO(arr_outputs[i],1); };
                    }
                else{
                    window.alert('Spatny navratovy kod pro AJAX');
                    } 
            })(i)   
        } 
    }


    function createHttpRequest() {
      if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        http_request = new XMLHttpRequest();
        } 
      else if (window.ActiveXObject) {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return http_request;
    }
    </script>

<a id="27" style="cursor: pointer; color:red;">OFF</a>
<a id="27" style="cursor: pointer; color:red;">ON</a>

<a id="21" style="cursor: pointer; color:red;">OFF</a>
<a id="21" style="cursor: pointer; color:red;">ON</a>

<a id="5" style="cursor: pointer; color:red;">OFF</a>
<a id="5" style="cursor: pointer; color:red;">ON</a>

<a id="9" style="cursor: pointer; color:red;">OFF</a>
<a id="9" style="cursor: pointer; color:red;">ON</a>
B.Ondrej
  • 381
  • 4
  • 14
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – doldt Aug 05 '15 at 09:26
  • I am confused. There are only function definitions in the sample you have given, no function call. So I am not sure what you mean by it running/not running. You'll need to provide more context in order to diagnose the problem. – musically_ut Aug 05 '15 at 09:26
  • The problem is that in IE onclick event does not trigger any action, but how to debug it, if console is empty? I added A HTML tags, the function trigger is always added dynamically over DOM, so onclick event is not visible in HTML code. Chrome, FF, Safari = no issues, only problem as usually is IE. – B.Ondrej Aug 05 '15 at 09:53

2 Answers2

1

UPDATE2: The problem is solved after days of searching. Solution: req.setRequestHeader( "Pragma", "no-cache" ); disables caching in my case and works. My assumption is that, the script worked in debug mode only, because caching was enabled and in console mode caching is force suppressed. However HTTPS caching is expected to be always disabled be default, but apparently there was something wrong with that.

B.Ondrej
  • 381
  • 4
  • 14
0

I have a suggest solution for it

1: Set run mode to under browser version if it version worked.

<meta http-equiv="X-UA-Compatible" content="IE=9"/>
HoangHieu
  • 2,802
  • 3
  • 28
  • 44