Warning: A call to document.write() from an asynchronously-loaded external script was ignored. How is this fixed?

久未见 提交于 2019-11-28 00:22:59

Don't use document.write. The script is being loaded asynchronously, which means it's detached from the document parsing state. There is quite literally NO WAY for the JS engine to know WHERE the document.write should be executed in the page.

The external script could load instantaneously and the document.write executes where the <script src="..."> tag is, or it could hit a net.burp and load an hour later, which means the document.write gets tagged at the end of the page. It's quite literally a race condition, so JS engines will ignore document.writes from scripts loaded asynchronously.

Convert the document.write to use regular DOM operations, guarded by a document.onload type handler.

If you have access to the .js file in question, your best solution is going to be to modify the "document.write()" method and replace it with whatever makes sense in order to distribute the content contained within.

The reasons for this are very well described above.

If you are using document.write to write html tags to the page:

document.write("<script src=...></script>");

or

document.write("<img href=... />");

Consider using the same sort of asynchronous format you've already been using:

// Add/Remove/Sugar these components to taste
script = document.createElement("script");
script.onload = function () { namespaced.func.init(); };    
script.src = "http://...";
document.getElementsByTagName("script")[0].parentNode.appendChild(script);

If you're looking to append DOM elements that are for the user to see and interact with, then you're better off either:

a) Grabbing a specific containter (section/div) by id, and appending your content:

document.getElementById("price").innerHTML = "<span>$39.95</span>";

b) Building content off-DOM and injecting it into your container:

var frag = document.createDocumentFragment(),
    span = document.createElement("span");
span.innerText = "39.95";
frag.appendChild(span);
document.getElementById("price").appendChild(frag);

Again, Sugar to your liking.

If you DON'T have access to mod this second .js file, I'd suggest taking it up with them.

I had the same problem loading google maps with the places library. I temporarily override the write function to create a new script element in the head.

(function() {
  var docWrite = document.write;
  document.write = function(text) {
    var res = /^<script[^>]*src="([^"]*)"[^>]*><\/script>$/.exec(text);
    if (res) {
      console.log("Adding script " + res[1]);
      var head = document.getElementsByTagName('head')[0];
      var script = document.createElement("script");
      script.src = res[1];
      head.appendChild(script);
    } else {
      docWrite(text);
    }
  }
})();

Now all I have to do to load a script asynchronously is

document.write('<script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!