在AMP中包含自定义JavaScript的最佳方法

问题描述 投票:15回答:6

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

我知道<script>标记是禁止的,除非其类型为application/ld+json

有一些默认的AMP HTML运行时组件和扩展的组件,其中包含针对不同组件的特定形式,但是我找不到用于自定义JavaScript的特定形式。

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

<script src="https://arifkarim.com/widget/layouts/global/js/legaltext.js"></script>
javascript amp-html
6个回答
30
投票

AMP的全部目的是仅允许一部分网络技术阻止您的页面运行缓慢。

Java脚本通常是导致网站运行缓慢的原因,因此AMP页面不允许它们(除了AMP脚本本身),尽管它们试图填补这部分空白,但这些amp组件是专门为不慢而编写的。

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

  1. 请勿使用AMP。没有人强迫你这样做。
  2. 从您的amp文档中删除脚本标签,并且没有该功能就可以使用。
  3. 找到一个与您的JavaScript相同的放大器组件,并改用它。我不知道我会猜出什么legaltext.js,因为它的名字显示了一些合法的文本,例如cookie通知,所以也许amp-user-notification小部件可以代替?
  4. amp iframe中使用您的Javascript。这些在amp页面中是允许的,但可能会以较低的优先级加载,以确保它们不会降低主页的速度。

4
投票

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


3
投票

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

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

<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>

1
投票

[好吧,我遇到了同样的问题,对我而言,最好的方法是使用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

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

希望有帮助!


0
投票

由于AMP本身支持amp-iframe中提到的javascript,现在无需使用official document

AMP页面通过<amp-script>组件支持自定义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>

0
投票

AMP允许在开发和生产模式下使用自定义JavaScript,而无需进行黑客攻击。

要在AMP页面中包含自定义JavaScript,请使用<amp-script>组件。

下面是说明如何使用<amp-script>组件的代码段;

<amp-script>

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

但是,AMP对任何给定页面上的所有<!doctype html> <html amp> <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> 组件都强制执行150 KB的自定义JavaScript限制,以保持性能。

© www.soinside.com 2019 - 2024. All rights reserved.