1

I've been trying to include a javascript file in AMP page but it fails each time i tried

<amp-iframe width="200" height="100px" 
                        layout="responsive" sandbox="allow-scripts allow-same-origin"
                        src="https://laraflat.000webhostapp.com/public/js/index2.js">

</amp-iframe>

but it fails to load the functions inside this script

Sanchit Gupta
  • 3,148
  • 2
  • 28
  • 36
Rami
  • 161
  • 3
  • 18
  • 1
    Possible duplicate of [Add Javascript in AMP Pages](https://stackoverflow.com/questions/43748634/add-javascript-in-amp-pages) – Daniel Beck Jan 27 '18 at 15:21
  • i applied that answer but it didn't help – Rami Jan 27 '18 at 15:22
  • 1
    That's because the answer is "you can't do that." Read the [spec](https://www.ampproject.org/docs/reference/spec#html-tags) : "AMP HTML uses a set of contributed but centrally managed and hosted custom elements to implement advanced functionality such as image galleries that might be found in an AMP HTML document. While it does allow styling the document using custom CSS, **it does not allow author written JavaScript** beyond what is provided through the custom elements to reach its performance goals." – Daniel Beck Jan 27 '18 at 15:24
  • but the documentation also says that i can add javascript through using amp-iframe – Rami Jan 27 '18 at 15:29
  • See https://stackoverflow.com/questions/35248005/can-i-enable-js-if-i-embed-the-real-page-inside-an-amp-iframe – Daniel Beck Jan 27 '18 at 16:09

1 Answers1

1

AMP Prohibits the use of external javascript libraries. You can only make use of the native AMP component's script.

JavaScript is powerful, it can modify just about every aspect of the page, but it can also block DOM construction and delay page rendering (see also Adding interactivity with JavaScript). To keep JavaScript from delaying page rendering, AMP allows only asynchronous JavaScript.

For more information visit here

Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65