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');
        });
Jackson Cunningham
  • 4,973
  • 3
  • 30
  • 80
  • 1
    Possible duplicate of [trying to fire onload event on script tag](http://stackoverflow.com/questions/16230886/trying-to-fire-onload-event-on-script-tag) – Jared Smith Feb 23 '17 at 01:40

1 Answers1

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!

tin
  • 137
  • 1
  • 8
  • try selecting with just document.querySelector('script[src*="analytics"]') – tin Feb 23 '17 at 01:49
  • 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. – Jackson Cunningham Feb 23 '17 at 01:52
  • 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! – tin Feb 23 '17 at 02:03