AMP allow the use of custom JavaScript in both development and production mode without necessitating a (iframe) hack.
To include a custom JavaScript in your AMP page, make use of the <amp-script>
component.
Below, a snippet illustrating how to use the <amp-script>
component;
<!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>
Note that the reference to your custom JavaScript can either be a relative or full path.
It is good to note that AMP enforces a limit of 150 kilobytes of custom JavaScript for all <amp-script>
component on any given page.
The reason for the 150 kilobytes of custom JavaScript for all <amp-script>
component is to keep performance in check.
application/ld+json
tags are allowed. Script tags are prohibited except the mandatory script tag to load the AMP runtime and the script tags to load extended components. You can see the specific form for custom js in Allowed and Prohibited AMP html tags documentation. – Sandrocottus