在AMP中运行JavaScript [英] Running JavaScript in AMP

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

问题描述

我对应该如何在AMP页面中运行JavaScript感到有些困惑.

I am a bit confused as to how JavaScript is suppposed to be run in an AMP page.

我了解到我的JavaScript必须在iframe中执行.此类iframe必须放置在页面中(至少从顶部放置75%),并且必须通过https进行投放.这确实有效:

I got as far as understanding that my JavaScript must be executed in an iframe. Such iframe has to be placed down in the page (75% at least from top) and has to be served through https. This does indeed work:

<amp-iframe 
  width=300 
  height=300 
  sandbox="allow-scripts allow-popups allow-popups-to-escape-sandbox" 
  layout="responsive"
  frameborder="0" 
  src="https://localhost:8000/?p=myjs">
</amp-iframe>

在该页面(https://localhost:8000/?p=myjs)中,我可以自由运行我的js.

In that page (https://localhost:8000/?p=myjs) I can freely run my js.

我的问题是以下问题:

我应该如何对包含iframe的主页文档运行代码?

How am I supposed to run my code against the document of the main page including the iframe?

我尝试访问window.parent.document,但被阻止了. (当然).

I tried accessing window.parent.document and that is blocked. (of course).

谁能解释AMP人士如何认为如果我们所有的js被杀死,我们实际上可以将页面移植到AMP?推荐的模式让我们的js在AMP页面上运行是什么?有什么问题吗?还是只是假设开发人员必须转储所有代码?

Can anyone explain how AMP people think we can actually port pages to AMP if all our js gets killed? What is the recommended pattern to have our js run on an AMP page? Is there any such thing or is it just assumed that developers must dump all their code?

谢谢

推荐答案

我应该如何对包含iframe的主页文档运行代码?

How am I supposed to run my code against the document of the main page including the iframe?

你不是.

有人能解释AMP人们如何认为如果我们所有的js被杀死,我们实际上可以将页面移植到AMP吗?

Can anyone explain how AMP people think we can actually port pages to AMP if all our js gets killed?

想法是您编写不依赖JavaScript的轻量级页面.

The idea is that you write lightweight pages that don't depend on JavaScript.

来自规范:

AMP HTML是HTML的一个子集,用于编写内容页面(例如新闻报道),以保证某些基准性能特征.

诸如新闻文章之类的内容页面"不需要JavaScript.如果您不编写此类页面,则AMP HTML可能不是一种不错的语言选择,您应该坚持使用HTML 5.

"Content pages such as news articles" don't need JavaScript. If you aren't writing that sort of page then maybe AMP HTML isn't a good choice of language and you should stick to HTML 5.

这篇关于在AMP中运行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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