I am trying to async the google map api javascript.
So, the normal script tag works <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
But, the following async version doesn't.
(function () {
var gmap = document.createElement('script'); gmap.type = 'text/javascript'; gmap.async = true;
gmap.src = 'https://maps.googleapis.com/maps/api/js?sensor=false';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gmap, s);
})();
After some breakpoint+inspect action, I found out that this line doesn't run properly in the async mode.
document.write('<' + 'script src="' + src + '"' +
' type="text/javascript"><' + '/script>');
The document object in the sync mode is a "HTMLDocument", but in the async mode is a "#document" instead. Something happened to the document object after the page is loaded. Thoughts?
Cheers.
Update: this question is more about why document.write is not fired rather than async load the google map api. If you set a breakpoint on this line, you can see document.write function exists. Does this have anything to do with the fact that document.write is native?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…