Add Javascript in AMP Pages

前端 未结 5 1325
灰色年华
灰色年华 2020-12-06 02:37

Please help me understand how to add javascript in the AMP (Accelerated Mobile Pages). My requirement is I get an ID in URL. For example localhost:8080/home?id=1.I would lik

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 02:59

    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:

    1. At the top of your .html file import:
    
    
    1. Wrap the html element/s with the amp-script component:
    
    
      
    
    
    1. 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  element.
      document.body.appendChild(el);
    });
    

    You can find more details and how it works in the AMP git repo amp-script.md

提交回复
热议问题