Checking template name from script

Excursionist
19 0 2

Hi

 

Im wanting a script to set some values on the products page only - the problem is how do I guarantee this in my design

 

The options as I see it -

 

  1. Check url - example.com/products
  2. template.name - Isnt guaranteed
  3. data-section-id={{section.id}}

My preferred option would be 3 - but Im not convinced I can guarantee this, so option 1 would be best?

 

Any advice?

0 Likes
Shopify Partner
90 8 10

@Martin37 

Can you share your store URL? I understand you want to add content based on-page. you can add it with Shopify liquid code there is no need for the script.

If you want to go with link-based javascript then you can try the below code.

  if( window.location.pathname == "/collections/all" ){
      alert('page display');
    }
Thanks & Regards
Akshay Vaghasiya

Shopify Partner | Shopify Experts
Email: akshayvaghasiya84@gmail.com
Skype: akshayvaghasiya84
 - Want to modify or custom changes on store hire me .
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
0 Likes
Excursionist
19 0 2

Hi

 

Thanks for your response 

 

Its more general than a theme, and more how to identify a template from a script. Im not convinced you can guarantee it, although good practice might advise it(certainly the case in shopify's themes)

 

Best approach would be the URL, unless someone can advise different?

 

Thanks

0 Likes
Shopify Partner
90 8 10

@Martin37 

If you want to identify template then you need to use

{% if template == 'index' %}

// Paste your code  here

{% endif %}

 

But it will only work with a liquid file like theme.liquid. You can use it with theme.js or any .js file because it does not support template conditions.


Now maybe you got the answer.

Thanks & Regards
Akshay Vaghasiya

Shopify Partner | Shopify Experts
Email: akshayvaghasiya84@gmail.com
Skype: akshayvaghasiya84
 - Want to modify or custom changes on store hire me .
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
0 Likes