頁面引入百度地圖API:
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3&ak=XhxWo4lQh2KfH3phyElA17ljQC1wwqjT"></script>
產(chǎn)生如下問題:
api:1 A Parser-blocking, cross-origin script, http://api.map.baidu.com/getscript?v=1.3&ak=XhxWo4lQh2KfH3phyElA17ljQC1wwqjT&services=&t=20150527115231, is invoked via document.write. This may be blocked by the browser if the device has poor network connectivity.
即為報錯:A Parser-blocking, cross-origin script, is invoked via document.write.
在頁面渲染完成后就不能使用 document.write 方法豌蟋。
根據(jù)博客的說明嘗試如下方法:
<script>
//異步加載百度地圖,解決谷歌等瀏覽器在頁面渲染完成后就不能使用 document.write 方法的問題:
var cnzz_s_tag = document.createElement('script');
cnzz_s_tag.type = 'text/javascript';
cnzz_s_tag.async = true;
cnzz_s_tag.charset = 'utf-8';
cnzz_s_tag.src = 'http://api.map.baidu.com/api?v=1.3&ak=XhxWo4lQh2KfH3phyElA17ljQC1wwqjT&async=1';
var root_s = document.getElementsByTagName('script')[0];
root_s.parentNode.insertBefore(cnzz_s_tag, root_s);
</script>
出現(xiàn)問題:
api:1 Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
后來發(fā)現(xiàn)頁面引入的百度的js內(nèi)容為:
(function(){
window.BMap_loadScriptTime = (new Date).getTime();
document.write('<script type="text/javascript" src="http://api.map.baidu.com/getscript?v=1.3&ak=XhxWo4lQh2KfH3phyElA17ljQC1wwqjT&services=&t=20150527115231"></script>');
document.write('<link rel="stylesheet" type="text/css" />');
})();
里面包含了 document.write方法淆珊,異步加載的js是不允許使用document.write方法的
直接引用這兩個地址的jshttp://api.map.baidu.com/getscript?type=quick&file=api&ak=o9B4Ol99j9NcBXSu5nFTR7uI&t=20140109092002
和 http://api.map.baidu.com/getscript?type=quick&file=feature&ak=o9B4Ol99j9NcBXSu5nFTR7uI&t=20140109092002
就可以了