FROM CACHE - en_header

How to tell if current page is Shopping Cart Page (in Javascript)?

Matt28
Shopify Partner
15 0 0

Hi guys,

Just wondering if there is a way to determine whether the current page is the "Shopping Cart" page in Javascript?  Can I assume all shopping cart pages will be /cart, or is it more complicated than that?

Thanks!

Matt.

Replies 3 (3)
Jason
Shopify Expert
10918 192 2190

The easiest way would be to just look at the current url - being /cart. Now some themes don't often direct users to that page if they use AJAX carts so you'd need to adapt your approach in that instance.

What's the use case?

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
Matt28
Shopify Partner
15 0 0

Thanks Jason - I'd like to do some checking on my server (via Ajax call) if the user is hitting the cart page... would have preferred to do it on the checkout page, but alas, no javascript is allowed on there from what I've read 🙂

It's easy to check for the "/cart" url, but as you mentioned, in the case of other implementations of the cart, it may be an issue.

Thanks,

Matt.

bchesler
Tourist
3 0 0

Just in case anyone else lands here, you can request the page type. In this case:

{% if request.page_type == 'cart' %}
    do something
{% endif %}

https://shopify.dev/docs/themes/liquid/reference/objects/request#request-page_type