Javascript 通过 document.write 调用一个 Parser-blocking、跨域脚本——如何规避它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39610829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:44:09  来源:igfitidea点击:

A Parser-blocking, cross-origin script is invoked via document.write - how to circumvent it?

javascriptgoogle-chromedocument.write

提问by niutech

Google Chrome started implementing Blocking the load of cross-origin, parser-blocking scripts inserted via document.write in the main frameon slow networks, which causes the following error:

谷歌浏览器开始在慢速网络的主框架中实施阻止加载跨域解析器阻止脚本的加载,通过 document.write 插入,导致以下错误:

A Parser-blocking, cross-origin script, http://example.org/script.js, is invoked via document.write. This may be blocked by the browser if the device has poor network connectivity.

通过 document.write 调用解析器阻塞、跨源脚本http://example.org/script.js。如果设备的网络连接不佳,这可能会被浏览器阻止。

However, my web page requires loading a third party script synchronously, using document.write('<script src="..."></script>'). How to circumvent that blockade?

但是,我的网页需要同步加载第三方脚本,使用document.write('<script src="..."></script>'). 如何绕过这个封锁?

More about that change:

有关该更改的更多信息:

采纳答案by niutech

According to Google Developers article, you can:

根据Google Developers 文章,您可以:

回答by Vaibhav Mistry

@niutech I was having the similar issue which is caused by Rocket Loader Moduleby Cloudflare. Just disable it for the website and it will sort out all your related issues.

@niutech 我遇到了由 Cloudflare 的Rocket Loader 模块引起的类似问题。只需为网站禁用它,它就会解决您所有的相关问题。

回答by Gray

Don't use document.write, here is workaround:

不要使用 document.write,这是解决方法:

var script = document.createElement('script');  
script.src = "....";  
document.head.appendChild(script);