0

I'm trying to figure out how to convert below code to Javascript. Is there a conversion tool online for simple conversions?

$('head script[async][src*="analytics"]').on('load', function() {
          ga('send', 'event', 'checkout', 'complete');
        });
1

1 Answer 1

2

I don't know if there's a conversion tool, but there is http://youmightnotneedjquery.com which holds resources for many things one can do without jQuery, compared with the jQuery way.

In your case just replace $() with document.querySelector and .on('load', fn) with .onload = fn i.e:

document.querySelector('head script[async][src*="analytics"]').onload = function() {
      ga('send', 'event', 'checkout', 'complete');
    }

Let me know if it works!

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

3 Comments

try selecting with just document.querySelector('script[src*="analytics"]')
Same thing "Uncaught TypeError: Cannot set property 'onload' of null". Problem is that Shopify is loading the script so I can't edit it... and it seems to be taking a few seconds to load.
I couldn't tell you why but what I see is that when executing querySelector it's not finding the script element with that selector . Try setting a breakpoint with DevTools on that line and check from the console document.scripts to see if it's there. Or find another selector to match that script. Good look!

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.