Skip to main content
Version: 1.0.0

Building URLs in JavaScript

BASE_URL

In YUI themes the yui/url module is used to prepend a URL path with the current store base URL. With YSCP it is much simpler: the window.BASE_URL variable can be used.

For regular requests, use:

BASE_URL + "example/path/action"

or the template literal version:

`${BASE_URL}example/path/action`

The above is the eqivalent to what the YUI urlBuilder.build() method does.

A alternative option is to render the base URL with Twig:

"{{ craft.app.sites.getCurrentSite().getBaseUrl() }}example/path/action"

There is no "better" or "worse" way to do this.

CURRENT_STORE_CODE

If you need to add the store code to the request path, for example for Ajax requests, use:

`${BASE_URL}/rest/${CURRENT_STORE_CODE}/example/path/action`

Or, the non-template literal version:

BASE_URL + 'rest/' + CURRENT_STORE_CODE + '/example/path/action'