Rachel Cantor
Rachel Cantor

Reputation: 336

How can you detect if current page is AMP?

I'm new to AMP and we only want something to happen if the current page is not an AMP page. Is there a universal property on window that we can see if it's defined in order to detect if the current view is AMP or not?

Some background: we're currently delivering a react app through a wordpress plugin and we're noticing some errors that might be from AMP pages only (even though we're not approved to run on AMP pages yet - not sure why our script is still attempting to load), so we'd like to not run our script if the current view is an AMP page.

Upvotes: 2

Views: 5444

Answers (4)

Himanshu Pandey
Himanshu Pandey

Reputation: 708

If you are using chrome browser then install below extension:

AMP Validator(https://chrome.google.com/webstore/detail/amp-validator/nmoffdblmcmgeicmolmhobpoocbbmknc?hl=en)

If current page is using AMP then this extension become active in browser.

Upvotes: -3

user2468968
user2468968

Reputation: 286

There is the "AMP" property after the initialization of the AMP page. This test is more accurate than merely checking for the amp markup.

Beware that adding any custom javascript to do such detections will invalidate the AMP page. The page may still function on its own but will not be able to exploit the AMP-cache or search engine enhancements.

Upvotes: 1

Loren Rosen
Loren Rosen

Reputation: 125

Not onwindow, but the AMP spec requires the html tag to have either the or amp attribute, that is, either <html ⚡> or <html amp>. It also requires <style amp-boilerplate> inside head. Should be easy to use JavaScript to check.

For the spec statements, see https://www.ampproject.org/docs/fundamentals/spec#required-markup

Upvotes: 1

Rachel Cantor
Rachel Cantor

Reputation: 336

I ended up seeing that the <html> tag tends to have an attribute amp when it's an AMP page. I'm not sure if it's always the case, but if anyone has the same question, this seems to work for my use-case: document.getElementsByTagName('html')[0].hasAttribute('amp')

Upvotes: 4

Related Questions