> For the complete documentation index, see [llms.txt](https://foxecom.gitbook.io/zest-theme/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://foxecom.gitbook.io/zest-theme/faqs/generic/javascript-events-for-developers.md).

# JavaScript events for developers

Zest leverages JavaScript extensively to optimize loading times and ensure seamless performance of features, such as the quick view.

In certain cases, you may need to capture these events after they occur, enabling you to execute custom scripts or integrate third-party applications.

All objects returned are vanilla JS objects and can be edited however you see fit.

## 1. **Page loaded** <a href="#id-1.-page-loaded" id="id-1.-page-loaded"></a>

The event is triggered when the HTML document has been fully parsed, and all deferred scripts have finished downloading and running.

```javascript
document.addEventListener('page:loaded', function() {
  // Page has loaded and theme assets are ready
})
```

## 2. Product successfully added to the Ajax cart <a href="#id-2.-product-successfully-added-to-the-ajax-cart" id="id-2.-product-successfully-added-to-the-ajax-cart"></a>

[Product object returned](https://shopify.dev/docs/api/liquid/objects/product).

```javascript
document.addEventListener('product-ajax:added', function(evt) {
  console.log(evt.detail.product);
});
```

## 3. Product could not be added to the Ajax cart <a href="#id-3.-product-could-not-be-added-to-the-ajax-cart" id="id-3.-product-could-not-be-added-to-the-ajax-cart"></a>

The error message will let you know the problem. Most of the time it is quantity related.

```javascript
document.addEventListener('product-ajax:error', function(evt) {
  console.log(evt.detail.errorMessage);
});
```

## **4.** Cart updated <a href="#id-4.-cart-updated" id="id-4.-cart-updated"></a>

JavaScript updates the cart object once the quantity is modified. This event is triggered after the cart markup has been updated and is open to customization.

[Cart object returned](https://help.shopify.com/en/themes/liquid/objects/cart).

```javascript
document.addEventListener('cart:updated', function(evt) {
    console.log(evt.detail.cart);
});
```

## 5. **Quick view modal has been opened** <a href="#id-5.-quick-view-modal-has-been-opened" id="id-5.-quick-view-modal-has-been-opened"></a>

The first time the modal is opened, certain elements, like the add-to-cart form, are lazy-loaded. These elements are not immediately available.

```javascript
document.addEventListener('quick-view:open', function(evt) {
  console.log(evt.detail.productUrl);
});
```

## 6. **Quick view modal has been loaded** <a href="#id-6.-quick-view-modal-has-been-loaded" id="id-6.-quick-view-modal-has-been-loaded"></a>

Only fires the first time the modal is opened.

```javascript
document.addEventListener('quick-view:loaded', function(evt) {
  console.log(evt.detail.productUrl);
});
```

## **7. Variant selection changed** <a href="#id-7.-variant-selection-changed" id="id-7.-variant-selection-changed"></a>

[Variant object returned](https://shopify.dev/docs/api/liquid/objects/variant).

```javascript
document.addEventListener('variant:changed', function(evt) {
    console.log(evt.detail.variant);
});
```

## **8. Collection page is refreshed** <a href="#id-8.-collection-page-is-refreshed" id="id-8.-collection-page-is-refreshed"></a>

Fired when the collection page is re-rendered after filters are updated.

```javascript
document.addEventListener('collection:rerendered', function() {
    console.log('collection refreshed');
});
```

## **9.** Trigger cart drawer to refresh <a href="#id-9.-trigger-cart-drawer-to-refresh" id="id-9.-trigger-cart-drawer-to-refresh"></a>

Set the `open` parameter to `false` if you don't want the cart drawer to be open after the update.

```javascript
document.dispatchEvent(new CustomEvent('cart:refresh', {
  bubbles: true, detail: { open: true }
}));
```
