1

I'm trying to use maps api (yandex but I have seen similar problem in google maps too) and include its script in head:

  <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>site</title>
      <meta charset="UTF-8">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/postscribe/2.0.8/postscribe.min.js">
      <script  async src="https://api-maps.yandex.ru/1.1/index.xml" type="text/javascript"></script>
      <script type="text/javascript">
        window.onload = function () {
          var map = new YMaps.Map(document.getElementById("YMapsID"));
          map.setCenter(new YMaps.GeoPoint(59.938518, 30.323342), 10);
        };
      </script>
    </head>

Here is body:

  <body>
<div class="map-block_map-item">
          <div id="YMapsID" style="width:100%;height:100%">
          </div>
</div>
</body>

But when page downloads I get this warning:

"It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened."

If I don't use async attribute get this message:

A parser-blocking, cross site (i.e. different eTLD+1) script, https://api-maps.yandex.ru/1.1/_YMaps.js?v=1.1.21-58, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See https://www.chromestatus.com/feature/5718547946799104 for more details

How can I solve this problem? As I understood it happens on very slow connections (but I have rather fast connection so why?) and it cause block document.written scripts. (I'm new in js so yes I have google it but didn't understand how it can be improve besides async script download)

Anton Chekmarev
  • 61
  • 1
  • 1
  • 3
  • It's true, you can't use document.write to write to a document that's already been written. Since loading the script asynchronously will attempt to do exactly that, you will have to remove the async attribute. However, removing that (and adding in the closing tag that's missing from the previous line) seems to work for me: https://codepen.io/anon/pen/VreLNY – delinear Nov 02 '17 at 14:22
  • We highly recommend to use the API version 2.1, because API 1.1 is deprecated for several years and there is no support for new browsers and devices. – Reni Dec 08 '17 at 15:18

0 Answers0