How to get page id from storefront API JS?

I am developing an app for the marketplace and I included js file to the storefront.

Could you please help me to figure out how to get page id?

Add the code in your js file

It’s return the page template name.

var pageTemplate = $(“body”).attr(‘class’);
console.log(pageTemplate );

Thanks.

If the body has few classes then I can’t check page id :frowning:
And this method doesn’t work when I navigate to thank you page.

var pageTemplate = $("body").attr('class');
if (pageTemplate === 'template-product') {
    // action
}

Example:

Okay, but that’s now just down to common JS stuff… I mean you can

// split your body classes in to an array and check if that contains the value
var classes = $('body').attr('class').split(' ');
if ($.inArray('template-product', classes) !== -1) {
  console.log('Yay, I am a Product Page');
}

// or simply check the substring exists
var clazz = $('body').attr('class');
if (clazz.indexOf('template-product') !== -1) {
  console.log('Yay, I am a Product Page too');
}

p.s. I was asleep when answering beforehand.

if ($('body').hasClass('template-product')) { ... }

Doesn’t get easier than that :stuck_out_tongue:

1 Like