async loading javascript with [removed]

后端 未结 5 1030
野趣味
野趣味 2020-11-29 12:08

I am trying to async the google map api javascript.

So, the normal script tag works

5条回答
  •  囚心锁ツ
    2020-11-29 12:43

    I just ran into a very similar problem when asynchronously loading amazon ads. I was able to get document.write approximated in my app for these situations by changing its behavior ($ in this case refers to jQuery):

    document.write = function(content) {
      if (document.currentScript) {
        var src = document.currentScript.src
            .replace(/\#.*$/, '')
            .replace(/\?.*$/, '')
            .replace(/^.*\/\//, '');
        setTimeout(function() {
          var script = $('script').filter(function() {
            var scriptSrc = $(this).attr('src');
            return scriptSrc && scriptSrc.indexOf(src) !== -1;
          });
          $('
    ') .addClass('doc-write') .html(content) .insertAfter(script); }, 0); } else { HTMLDocument.prototype.write.apply(document, arguments); } };

    This approach could be improved on, but it works well enough for my needs. Hopefully you will find it useful.

提交回复
热议问题