admin管理员组文章数量:1191749
I am trying to async the google map api javascript.
So, the normal script tag works <script src=""></script>
But, the following async version doesn't.
(function () {
var gmap = document.createElement('script'); gmap.type = 'text/javascript'; gmap.async = true;
gmap.src = '';
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?
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?
Share Improve this question edited Oct 22, 2012 at 9:24 user1736525 asked Oct 22, 2012 at 0:35 user1736525user1736525 1,1291 gold badge10 silver badges15 bronze badges5 Answers
Reset to default 8document.write
can't be called from an asynchronous script, because it's detached from the document and therefore your JS parser doesn't know where to put it. at best, the browser will ignore it. at worst, it could write over the top of your current document (as in the case of calling document.write after the document has finished loading).
Unfortunately the only answer is to rewrite the script, which in the case of a google api is probably not a viable option.
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;
});
$('<div></div>')
.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.
When you use the parameter callback
inside the script-URL, the script doesn't use write()
and you'll be able to load the API asynchronously.
See: https://developers.google.com/maps/documentation/javascript/tutorial?hl=en#asynch
Do not use document.write()
, for the reasons explained by market and Dr Molle. Use appendChild()
instead, as done in Google's example of async loading.
Adding a comment here since I struggled a fair bit with this problem. When loading the script asynchronously (via a button click for example), make sure you follow the instructions exactly as on the site.
I first got the document.write
error when I didn't give the callback value. And after giving the callback, I got the window.initialize
error because ... duh ... there was no initialize function in my code. I changed that to my function name (something like loadMap) and it started working.
To be honest, just copy the code from the site and it should work. Replace window.onload
with whatever you need to trigger the said function.
本文标签: google maps api 3async loading javascript with documentwriteStack Overflow
版权声明:本文标题:google maps api 3 - async loading javascript with document.write - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738393603a2084379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论