gpt4 book ai didi

javascript - HTML 中允许 和 之外的 <script> 标签吗?

转载 作者:太空宇宙 更新时间:2023-11-04 16:28:55 25 4
gpt4 key购买 nike

基于这篇博文 Optimizing the asynchronous Google Analytics snippet和评论,我提出了这个优化的 Google Universal Analytics 片段,它更短,运行速度更快(尽管略微)并且破坏了 IE6/IE7/oldIE8 *兼容性。

+function(G,o,O,g){G.GoogleAnalyticsObject=O;G[O]||(G[O]=function(){(G[O].q=G[O].q||[]).push(arguments)});G[O].l=+new Date;g=o.createElement('script');g.src='//www.google-analytics.com/analytics.js';o.documentElement.appendChild(g)}(this,document,'ga');
ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');

但是,当上面的代码片段放在 <head> 中时, 它将 Universal Analytics 脚本作为不属于 <head> 的子脚本插入, 不属于 <body> , 但属于 <html> , 就在 </head> 之间和 <body> .

script between closing head tag and opening body tag

虽然 IE8+ 和现代浏览器似乎没有问题,但我仍然担心它可能会破坏旧的移动浏览器或其他一些晦涩的非 IE 浏览器。

HTML 规范中是否有描述浏览器应如何对放置在 <head> 之外的标签使用react的地方?和 <body>

* Windows XP 上未打补丁的 vanilla IE8 将像 IE6 和 IE7 一样中止页面加载并喷出 HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)。错误。 After installing a cumulative security update, starting with KB980182 first released in April 2010 and included in subsequent updates (KB982381, KB2183461, KB2360131, KB2416400, KB2482017, KB2497640, KB2530548, KB2559049, KB2586448, KB2618444, KB2647516, KB2675157, KB2699988, KB2722913, KB2744842 , KB2761465, KB2792100, KB2809289, KB2817183, KB2829530, KB2838727, KB2846071, KB2862772, KB2870699, KB2879017, KB2888505, KB28987859, 2KB20), 修复问题

最佳答案

最好在底部有 Javascript 脚本标签

  • 因为这些脚本会阻止并行下载。
  • 当脚本正在下载时,浏览器不会开始任何其他下载。
  • 有助于更快地加载页面

引用 yahoo developers

关于javascript - HTML 中允许 <head> 和 <body> 之外的 &lt;script&gt; 标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23382115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com