在AMP中包含自定义JavaScript的最佳方法 [英] Best way to include custom JavaScript in AMP

查看:126
本文介绍了在AMP中包含自定义JavaScript的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关脚本标记的所有文档,但找不到如何在AMP HTML中包含自定义JavaScript.

I read all documentation about script tag but I cannot find how to include a custom JavaScript in AMP HTML.

我知道<script>标记是禁止的,除非其类型为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的全部目的是仅允许一部分网络技术阻止您的网页运行缓慢.

The whole point of AMP is to only allow a subset of web technologies to stop your page being slow.

Javascript通常是导致网站运行缓慢的原因,因此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:

  1. 请勿使用AMP.没有人强迫你这样做.
  2. 从您的amp文档中删除脚本标签,并且在没有该功能的情况下仍然有效.
  3. 找到一个与JavaScript相同的放大器组件,并改用它.我一无所知,我会猜测它的名称显示了某些法律文本(例如Cookie通知),因此 amp iframe 中使用Javascript.这些内容在amp页面中是允许的,但可能会以较低的优先级加载,以确保它们不会降低主页的速度.
  1. Don't use AMP. Nobody is forcing this on you.
  2. Remove the script tag from your amp document and live without that functionality.
  3. 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?
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆