在 AMP 中包含自定义 JavaScript 的最佳方式 [英] Best way to include custom JavaScript in AMP
问题描述
我阅读了所有关于 script 标签的文档,但我找不到如何在 AMP HTML 中包含自定义 JavaScript.
I read all documentation about script tag but I cannot find how to include a custom JavaScript in AMP HTML.
我知道 标签是被禁止的,除非它的类型是
application/ld+json
.
I know the <script>
tag is prohibited unless its type is application/ld+json
.
有默认的 AMP HTML 运行时组件和包含不同组件的特定形式的扩展组件,但我找不到自定义 JavaScript 的特定形式.
There are default AMP HTML runtime components and extended components which contain specific form for different components, but I could not find a specific one for custom JavaScript.
这是我想包含在 AMP HTML 中的脚本标记;
Here is the script tag I want to include in AMP HTML;
<script src="https://arifkarim.com/widget/layouts/global/js/legaltext.js"></script>
推荐答案
AMP 的全部意义在于只允许 Web 技术的一个子集阻止您的页面变慢.
The whole point of AMP is to only allow a subset of web technologies to stop your page being slow.
Javascript 通常是网站缓慢的原因,因此 AMP 页面不允许它们(AMP 脚本本身除外),尽管他们试图填补这留下的空白,这些 amp 组件是专门编写的慢.
Javascript is often the cause of slow websites and so AMP pages do not allow them (except for the AMP scripts themselves), though they've tried to fill in the gap this leaves with amp components which are specially written to not be slow.
因此,如果您想使用 Javascript,您有多种选择:
So if you want to use Javascript you've several choices:
- 不要使用 AMP.没有人强迫你这样做.
- 从您的 amp 文档中删除脚本标签,并在没有该功能的情况下继续使用.
- 找到一个与您的 JavaScript 具有相同功能的 amp 组件并使用它.不知道是什么 legaltext.js 我猜它会显示一些法律文本,比如 cookie 通知,所以也许 amp-user-notification 小部件可以代替吗?
- 在 amp iframe 中使用您的 Javascript.这些在 amp 页面中是允许的,但可能会以较低的优先级加载,以确保它们不会减慢主页面的速度.
- Don't use AMP. Nobody is forcing this on you.
- Remove the script tag from your amp document and live without that functionality.
- Find an amp-component which does the same as your JavaScript and use that instead. Not having a clue what legaltext.js I would guess by the name it displays some legal text like a cookie notice so maybe the amp-user-notification widget would work instead?
- Use your Javascript in an amp iframe. These are allowed in amp pages but will presumable be loaded with a lower priority to ensure they don't slow down the main page.
这篇关于在 AMP 中包含自定义 JavaScript 的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!