Google AMP - Save current page URL to local storage

865 Views Asked by At

Can anyone point me in the right direction? I'm building a landing page that is supposed to store the current page URL with query strings to local storage. I've looked all over, but can't find a solution that will keep my AMP page valid.

3

There are 3 best solutions below

3
On BEST ANSWER

This is not possible with AMP as you can't run custom Javascript. This means you won't be able to read from local storage either.

0
On

Nowadays you can use for custom JS and access the localStorage, but as far as I know, you can't access the page URL directly.

Look at this compatibility table: https://github.com/ampproject/worker-dom/blob/main/web_compat_table.md

Document.location is marked as false.

I'm working in an AMP project that needed the current page URL. What I did was to save the URL as an attribute and use a custom JS to read from this attribute (my pages are rendered in the server, so I can add this info there).

0
On

I know this is a 3 year old question, but what you're trying to accomplish is doable through the use of the amp-script component.

You can read up here: https://amp.dev/documentation/components/amp-script/

Hope this helps somebody in the future.