J
J
JackShcherbakov2018-02-20 20:49:09
Yandex browser
JackShcherbakov, 2018-02-20 20:49:09

Why does alert appear for a few seconds in Yandex, but not in other browsers?

Hello! There was no JSONP tag, so I just put JSON.
The problem is this:
There is a script that sends a request to my server in php using the JSONP protocol:

var CallbackRegistry = {};

function scriptRequest(url, onSuccess, onError) {

  var scriptOk = false; 
  var callbackName = 'cb' + String(Math.random()).slice(-6);

  url += ~url.indexOf('?') ? '&' : '?';
  url += 'callback=CallbackRegistry.' + callbackName;

  CallbackRegistry[callbackName] = function(data) {
    scriptOk = true; 
    delete CallbackRegistry[callbackName]; 
    onSuccess(data); 
  };

  function checkCallback() {
    if (scriptOk) return; 
    delete CallbackRegistry[callbackName];
    onError(url); 
  }

  var script = document.createElement('script');

  script.onreadystatechange = function() {
    if (this.readyState == 'complete' || this.readyState == 'loaded') {
      this.onreadystatechange = null;
      setTimeout(checkCallback, 0); 
    }
  }

  script.onload = script.onerror = checkCallback;
  script.src = url;

  document.body.appendChild(script);
}
function ok(data) {
  alert( "Загружен время " + data.time );
}

function fail(url) {
  alert( 'Ошибка при запросе ' + url );
}

scriptRequest("http://comet/php.php", ok, fail);

Here is the php script itself:
<?php
$d = new DateTime();
$response = json_encode(array(
  time => $d->format("r")
));
print $_GET["callback"] . "(" . $response . ");";
?>

If you open a js script in Yandex, then the alert will appear for a moment and immediately disappear. Everything is ok in chrome though.
My assumptions:
1. XMLHttpRequest blocks the main thread (if it is synchronous, I don't know how it works when loading scripts), so does alert. Perhaps there is a conflict due to the fact that first alert freezes the main thread, then freezes the thread and xmlHttpRequest.
How to fix? What caused the problem?
Thanks in advance to everyone for the replies!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question