Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to determine what page my script is run on

when my app is installed I create a script tag (through the shopify API). I am only interested in running this script on product view pages. what is a reliable way to check whether the script is run by a product view page? Should I ask the merchant to put something in to their liquid file manually?

like image 800
akonsu Avatar asked Oct 06 '22 12:10

akonsu


1 Answers

This should work, in your JavaScript:

// If we are on a product page
if (window.location.pathname.indexOf('/products/') !== -1) {
 // What's the product handle?
 var productHandle = pathname.match(/\/products\/([a-z0-9\-]+)/)[1];
}
like image 129
Caroline Schnapp Avatar answered Oct 10 '22 03:10

Caroline Schnapp