0

I've looked through a lot of questions and I can't find a solution that works well within my environment. This is done within Shopify, I have a product with variants. When a variant is changed, the URL is appended with a product ID which changes the product price dynamically (no reload).

I have an extra strip detecting when this change happens so I can so some extra stuff, and currently, it does detect it but it seems like wasted power the route I took with it.

$(document).ready(function() {
    setInterval(function() {
      checkURLchange(window.location.href);
    }, 10);

It happens every 10 ms so the change seems instant to the user (let's say I appended a new picture to the page or something on variant change).

It would be more much effective if I could just save the state of the URL and check if it has changed, but again Im unaware to approach this.

The typical hashChanged event for window does not work in this situation because the url change does not use hashes (Shopify), instead it looks something like this:

?variant=99390743207991

or

?variant=13390743202341

What about popstate (history change)?

Unfortunately, this does not seem to work either. As popstate or the answer suggested here.

Feel free to see the solution being tested in my chaotic staging environment: https://ti560ye2q51lxyvj-11502512.shopifypreview.com/products/the-wg-m1-grill-kit

3

1 Answer 1

1

Listen to window for popstate.

window.addEventListener('popstate', () => {
    /* do something */
})

You can test this by adding a console log to it and changing the hash with window.location.hash = 'somehash'.

Sign up to request clarification or add additional context in comments.

1 Comment

Sadly this solution does not work for me as well. I added in the event listener and when I changed the variant (in turn appending the URL) the event never trigged, but explicitly setting the hash within the console to test does trigger it. When this URL gets appended nothing gets reloaded. I was under the impression popstate/histroy is effective for navigation (new pages/hash etc).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.