Running JavaScript in AMP

I'm a little confused as to how JavaScript is supposed to run on an AMP page.

I realized that my JavaScript must be executed in an iframe. Such an iframe should be placed on the page (75% at least from the top) and should be sent via https. This really works:

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

On this page ( https://localhost:8000/?p=myjs) I can run js freely.

My problem is this:

How should I run my code against a main page document, including an iframe?

I tried to access window.parent.documentand blocked. (of course).

- , AMP , AMP, js ? js AMP? , ?

+8
5

, iframe?

.

- , AMP , AMP, js ?

, , JavaScript.

:

AMP HTML - HTML , , .

" , ", JavaScript. ​​, , AMP HTML , HTML 5.

+14

, AMP , JavaScript. , - , .

JS iframe, JS .

, , vue.js, AMP.

, AMP , JS. -. .

+8

AMP Conf 2018 JS AMP -. .

+6

Google, , amp . , "", , , Google . , :

- -. AMP , , AMP, Google, Bing, LinkedIn . , , , CTR.

. , , . . .

0

11 2019 . ,

JavaScript AMP-

AMP pages support custom JavaScript through the component <amp-script>.

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

Source: https://habr.com/ru/post/1658803/


All Articles