Frontend
Theme JS

Theme JS Overview

4min

Template JS Documentation

Klevu Template JS documentation can be found here https://docs.klevu.com/template-js.

Best Practice

Including Javascript

We have added 2 containers to the layout, one in head and one before body end. We recommend adding all Klevu related Javascript to these 2 contains. These 2 contains are named Klevu_Frontend.head.additional and Klevu_Frontend.before.body.end.

This makes it very easy to remove all Klevu scripts from a particular page. We already remove all Klevu Javascript from the checkout page, like so.

module-m2-frontend/view/frontend/layout/checkout_index_index.xml


Ensure Klevu is ready before accessing it

Calling any klevu methods before the Klevu JS has loaded will result in errors on the frontend. This may occur if there is a delay in loading the core Klevu library for any reason (e.g code being added being added higher up the page or async being added to the script tag).

It is always a good idea to wrap JS containing calls to klevu in window._klvReady.push(function(){}). This ensures the code wrapped inside _klvReady is not fired until klevu is available. e.g.

PHP


Note: window._klvReady.push is omitted from the code examples in the rest this documentation for brevity.