{"id":4369,"date":"2018-06-12T11:38:56","date_gmt":"2018-06-12T06:08:56","guid":{"rendered":"https:\/\/susanta.org\/?p=4369"},"modified":"2018-06-20T11:10:46","modified_gmt":"2018-06-20T05:40:46","slug":"google-amp-javascript-error","status":"publish","type":"post","link":"https:\/\/susanta.org\/google-amp-javascript-error\/","title":{"rendered":"User-authored JavaScript found on Page: Fix Google AMP Error"},"content":{"rendered":"

Recently, I got the following warning in Google Search Console:<\/p>\n

User-authored JavaScript found on page (Critical)<\/p>\n

Remove any custom JavaScript code from your page. AMP pages with custom JavaScript may appear in Google search results without AMP-specific display features.<\/p><\/blockquote>\n

When you\u2019re on the Google Console Dashboard, the screenshot might look some like this:<\/p>\n

\"Fix<\/p>\n

AND, Here\u2019s a screenshot of the warning.<\/p>\n

\"AMP<\/p>\n

When I clicked on one of the affected AMP pages, I could see the following window with details of the error message.<\/p>\n

\"How<\/p>\n

Although I could have easily ignored this warning but I hate seeing errors. So, I decided to dig in to figure out what might have triggered the errors.<\/p>\n

If you are seeing similar issues, read on to know how to fix it.<\/p>\n

Run AMP Validator<\/h2>\n

Although the warning already says your AMP page has some custom JavaScript, you need to run the AMP validator to establish the details of the issue and where exactly it appears in your page source.<\/p>\n

Therefore, you need to go to the AMP Validator tool<\/a> and enter the URL of the AMP page and hit on the \u201cValidate\u201d button.<\/p>\n

Note:<\/strong> The AMP Validator is different from AMP Test tool<\/a> which also tests if your AMP page is valid. It\u2019s important to keep this in mind because the AMP test tool will still say your page is valid (because you\u2019ve used AMP markup) while the AMP validator tool won\u2019t (due to the user authored javascript).<\/p>\n

The validator tool showed me the following error on the 27th line.<\/p>\n

<script>if (document.location.protocol != \u201chttps:\u201d) {document.location = document.URL.replace(\/^http:\/i, \u201chttps:\u201d);}<\/script><script type=\u2019text\/javascript\u2019 src=\u2019https:\/\/cdn.ampproject.org\/v0.js\u2019 async><\/script><\/p>\n