Add Javascript to Theme File, but will it get overwritten?

I’d prefer to add a single external js file to all my shopify pages, where I can just edit the file and know it will get to all pages properly.

However, all tutorials say I should just add the code (or the call to the script) in the theme.liquid file.

I am okay doing this, but won’t my edits get overwritten any time the theme gets updated? I’d prefer to avoid this, if this is the case.

What’s the best way to add an external js file call to ever page in shopify that won’t get overwritten?

Cheers!

Ryan

Hi @wooflinen

This is Noah from PageFly - Shopify Page Builder App

There are several ways to achieve the way like you want.

  1. You can call the script in each page by add your script in the template .liquid file of your page.

For example, i have about-us page with template about-us.liquid > then i will add the script to this liquid file so it will only be called on this page

  1. A second way is add the script by using liquid condition

For example my page is …/pages/about-us > so i add this code to theme.liquid but it only be called if it’s match the condition

{% if handle contains 'about-us' %}

//script called here

{%endif%}

Hope this can help you solve the issue

Best regards,

Noah | PageFly

It’s not if they will get overwritten it’s that if you change theme files then automatic upgrades may no longer work at all.

Use theme sections or settings when possible.

For OS2.0 compatible themes check the theme editors settings first.

Look for either a custom-liquid or custom-html section/block that is in the footer, or a global setting that accepts code. More extreme is just add a custom-liquid section to every template type with the javascript duplicated.

Some themes also specifically have a custom.js or similarly named asset file or template specifically for merchant javascript.

Good Hunting.

Thanks for the response. I’m not seeing any of that. I have my theme.liquid file requesting the script that I’m loading my code in. So, worse case scenario, I just have to re-add the call if the theme ever got updated.