As of 11th of April 2019 Official Announcement,
it is now possible using your JS inside an AMP project with amp-script
component.
First you need to import it to your project:
- At the top of your
.html
file import:
<script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
- Wrap the
html
element/s with the amp-script
component:
<!-- hello-world.html -->
<amp-script layout="container" src="https://yourdomain.com/hello-world.js">
<button id="hello">Insert Hello World!</button>
</amp-script>
- Now you can create the JS file
// hello-world.js
const button = document.getElementById('hello');
button.addEventListener('click', () => {
const el = document.createElement('h1');
el.textContent = 'Hello World!';
// `document.body` is effectively the <amp-script> element.
document.body.appendChild(el);
});
You can find more details and how it works in the
AMP git repo amp-script.md