【问题标题】:Best way to include custom JavaScript in AMP在 AMP 中包含自定义 JavaScript 的最佳方式
【发布时间】:2016-03-16 12:29:23
【问题描述】:

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

我知道<script> 标签是被禁止的,除非它的类型是application/ld+json。

有默认的 AMP HTML 运行时组件和扩展组件,它们包含不同组件的特定表单,但我找不到自定义 JavaScript 的特定表单。

这是我想在 AMP HTML 中包含的脚本标签;

<script src="https://arifkarim.com/widget/layouts/global/js/legaltext.js"></script>

【问题讨论】:

  • 您说得对,只允许使用不可执行的脚本标签,例如application/ld+json 标签。禁止使用脚本标签,但加载 AMP 运行时的强制脚本标签和加载扩展组件的脚本标签除外。自定义js的具体形式可以在Allowed and Prohibited AMP html tags文档中看到。
  • 这是我知道的,但我仍然在努力,但我还没有找到任何解决方案。无论如何,谢谢
  • 在 AMP 文档中,显然有一个自定义 JavaScript 部分。以下是推荐的添加方式:amp.dev/documentation/guides-and-tutorials/develop/…

标签: javascript amp-html


【解决方案1】:

AMP 的全部意义在于只允许一部分网络技术阻止您的网页变慢。

Javascript 通常是导致网站速度变慢的原因,因此 AMP 页面不允许使用它们(AMP 脚本本身除外),尽管他们试图用专门编写的 amp 组件来填补这一空白慢。

因此,如果您想使用 Javascript,您有多种选择:

  1. 不要使用 AMP。没有人强迫你这样做。
  2. 从您的 amp 文档中删除脚本标签,并在没有该功能的情况下继续使用。
  3. 找到一个与您的 JavaScript 功能相同的 amp-component 并改用它。不知道什么是legaltext.js,我猜它的名称会显示一些法律文本,例如cookie 通知,所以amp-user-notification 小部件可能会起作用?
  4. 在amp iframe 中使用您的Javascript。这些在 amp 页面中是允许的,但可能会以较低的优先级加载,以确保它们不会减慢主页速度。

【讨论】:

  • 您好,能否提供一个 AMP 页面示例,该示例使用 amp-iframe 加载 javascript?
  • 对于 #1 - 不要使用 AMP - 我想您仍然可以使用 AMP 并且仍然可以获得大部分速度优势,但您不会将其声明或验证为 AMP。 (在这种情况下,您可以将 AMP 视为实现快速网站的一组最佳实践和代码示例。)
  • @Tom 这真的是真的
  • @Tom 使用 AMP 的全部意义在于让 Google 缓存您的页面。如果您不打算将其声明或验证为 AMP,那么它就违背了整个目的。 AMP 当然不是一套最佳实践。
  • @nomad 我不敢相信你认为人们使用 AMP 主要是为了获得 SEO 的好处,而不是因为它是世界上最好的框架。 (“我很震惊——震惊地发现这里正在赌博!”,“你的奖金,先生”)。暗示这一点,就是暗示谷歌正在滥用其在市场上的主导地位。当然,如果他们不包括 SEO 的好处,那么 AMP 将同样成功,如果不是更多,基于它自己的优点? ;-)
【解决方案2】:

&lt;script&gt; 标记通常不允许在 AMP 中使用。有一些外部 javascript 文件,作为 AMP 项目的一部分构建,在某些情况下是允许的,甚至是必需的。除此之外,不允许使用 javascript。 AMP 无法使用自定义脚本标记。

【讨论】:

  • 您需要删除脚本标签。
【解决方案3】:

要在 AMP 页面中使用自定义 Javascript,您应该将其写入 Javascript 文件(例如:amp-iframe-0.1.js)。 然后将此脚本添加到&lt;head&gt;:&lt;script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"&gt;&lt;/script&gt;

可以使用 amp-iframe 调用自定义 javascript。 例如:

<amp-iframe width=300 height=300
    sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"
    layout="responsive"
    frameborder="0"
    src="https://www.google.com/maps/embed/v1/place?key=AIzaSyDG9YXIhKBhqclZizcSzJ0ROiE0qgVfwzI&q=Alameda,%20CA">
</amp-iframe>

【讨论】:

  • javascript在哪里被调用?
  • 我在此找不到 javascript
  • @xiaodai 请将脚本放入
  • @AdithyaSai javascript 被放置在 _amp-iframe 标签内
【解决方案4】:

现在不需要使用amp-iframe,因为 AMP 原生支持 official document 中提到的 javascript

AMP 页面通过 &lt;amp-script&gt; 组件支持自定义 JavaScript,如下所示:

<!doctype html>
<html ⚡>
<head>
  ...
  <script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
<body>  
  ...
  <amp-script layout="container" src="https://example.com/myfile.js">
    <p>Initial content that can be modified from JavaScript</p>
  </amp-script>
  ...
</body>
</html>

Here is official amp-script specs

【讨论】:

    【解决方案5】:

    好的,我遇到了同样的问题,对我来说最好的方法是使用 iframe,它会异步渲染。这确实意味着,您可以像这样解决它:

    服务器端 api: GET 请求(例如 /api/frames/my-js-script-app)。调用后会得到如下代码:

    <html>
    <head>
       <script defer src="your_js_scripts"></script>
    </head>
    <body>
      <!-- html code which using your javascript, if exists... --!>
    </body>
    </html>
    

    将 AMP 框架库添加到您的应用中:

     <head>
     ...
     <script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
     </head>
    

    现在,您可以在体内使用:

    <amp-iframe width=500 height=300
        sandbox="allow-scripts allow-same-origin"
        layout="responsive"
        frameborder="0"
        src="https://localhost/api/frames/my-js-script-app">
    </amp-iframe>
    

    在您的服务器上创建 api 时要小心。 AMP 框架需要 https 通信 - 这确实意味着:https://localhost/api/frames/my-js-script-app

    现在,amp 将异步渲染您的应用,每个人都很高兴 :-))

    希望对你有帮助!

    【讨论】:

      【解决方案6】:

      AMP 允许在开发和生产模式下使用自定义 JavaScript,而无需 (iframe) 破解。

      要在您的 AMP 页面中包含自定义 JavaScript,请使用 &lt;amp-script&gt; 组件。

      下面是一个说明如何使用&lt;amp-script&gt;组件的sn-p;

      <!doctype html>
      <html amp lang="en">
      <head>
        <meta charset="utf-8">
        <script async src="https://cdn.ampproject.org/v0.js"></script>
        <!-- Important to add "amp-script" custom element reference -->
        <script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
        <title>AMP with custom JavaScript</title>
        <link rel="canonical" href=".">
        <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
        <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
        </head>
      <head>
      <body>
        <amp-script layout="container" src="https://example.com/my-custom-javascript.js">
          <p>Here, content that you may want to modify using a custom JavaScript</p>
        </amp-script>
      </body>
      </html>
      

      请注意,对自定义 JavaScript 的引用可以是相对路径或完整路径。

      请注意,AMP 对任何给定页面上的所有 &lt;amp-script&gt; 组件强制执行 150 KB 的自定义 JavaScript 限制。

      为所有 &lt;amp-script&gt; 组件使用 150 KB 的自定义 JavaScript 的原因是为了控制性能。

      【讨论】:

        猜你喜欢
        • 2017-03-10
        • 1970-01-01
        • 1970-01-01
        • 2011-06-18
        • 2011-03-21
        • 2018-07-23
        • 1970-01-01
        • 1970-01-01
        • 2012-10-18
        相关资源
        最近更新 更多