Skip to main content
It is assumed that you are familiar with the templates of your shop and with the WEBSALE JavaScript library. Normally, the browser opens a follow-up page with every click on a link. With an AJAX integration (Asynchronous JavaScript and XML), the click instead calls a JavaScript function. This function starts the AJAX communication and prevents the browser from reloading the page. The requested content is loaded and inserted dynamically into the existing page. The benefit lies in reduced data traffic. A piece of content is only fetched when it is needed, for example when the buyer clicks on the “Question about the product” tab. The rest of the page remains untouched.
AJAX requires the customer to have JavaScript activated in their browser. If JavaScript is disabled, the page is fully rebuilt as usual. Your templates must therefore work in both states. The section on WS-Ajax sections shows how to achieve this.

Prerequisites

For all AJAX integrations you need the WEBSALE JavaScript system library together with its design component, at least in version 1.8.1. The system library brings the AJAX functions with it. In the design component you store your own reactions to success and errors. Guide: JavaScript and jQuery Materials: WEBSALE JavaScript library
If you use the tracking interface together with AJAX, follow the notes on AJAX-based functions in the tracking documentation. Since a full page build does not take place with an AJAX call, tracking calls otherwise do not take effect.Guide: Description of the tracking interface

How an AJAX call works

Before you build the first link, it is worth taking a look at the flow. It is the same for content and forms and explains why you specify four JavaScript functions.
  1. The customer clicks on a link or a button. The called function suppresses the normal page navigation.
  2. Before the request is sent, the startFunction runs. Here you typically show a layer with a loading graphic.
  3. The request goes to WEBSALE V8s. The requested template is generated there and returned.
  4. If no response is received, for example because the internet connection dropped or a timeout occurs, the failureFunction runs.
  5. If a response comes back, its content decides: on success, the successCallback runs; if WEBSALE V8s reports an error instead, the errorCallback runs.
  6. On success, the system library automatically inserts the returned content into the display areas of your page.
The difference between failureFunction and errorCallback is important for your error messages. The failureFunction stands for a communication error where the shop did not respond at all. The errorCallback stands for a response from the shop that reports an error, for example a template that was not found. The requested content is fetched via the WS-LoadTpl tag, possibly in combination with WS-ExternInclude, FORM-Template, or FORM-SSLTemplate. Ajax Load Tplnew

Loading content via AJAX

The simpler of the two cases is loading content, for example an information page behind a tab. It consists of four steps: build the link, include the library, create the display area, prepare the target template. The link calls the ws_AJAXloadTemplate function. The return in front prevents the browser from additionally following the link.
The meaning of the individual parameters can be found in the “Syntax of the functions” section. Reference: WS-LoadTpl()

Step 2: Include the system library and design component

Include the system library together with its design component. If you have already done so, check the version.
Then extend the design component with the four functions that you specified in the link. Without these functions, the call breaks as soon as the system library tries to invoke them.

Step 3: Define display areas

On the template with the link, create one or more display areas. The loaded content is inserted into them. Use any HTML element with an ID. The ID is the connection to the target template and must therefore be unique.

Step 4: Create WS-Ajax sections in the target template

WEBSALE V8s returns the template that the link calls. So that the system library knows which part of it belongs where, you divide the target template into WS-Ajax sections. The section name consists of the prefix WS-Ajax- and the ID of the display area. This way, you can exchange several areas at the same time with a single call.
Reference: WS-Ajax-”ID” If the same template is also reachable via a normal call, wrap the section markers with ST-Ajax. Without this bracketing, the markers would appear visibly in the source code on a normal call.
Reference: ST-Ajax

Loading and submitting forms via AJAX

A form goes through the same flow, but twice: once when the form is loaded and once when it is submitted. There is a separate function for submitting because the field contents are sent along. The result of the submission depends on the input. If it is faulty, WEBSALE V8s returns the form again, together with error messages. If it was valid, the confirmation page of the form comes back. In both cases the response ends up in the same display area. Loading the form is no different from loading a piece of content.

Step 2: Adapt the form and submit button

Integrate your form element as usual and assign an ID. Via this ID the function finds the field contents on submission. On the submit button, add the call to ws_AJAXsendFormTemplate.

Step 3: Extend the design component

Include the system library together with its design component and add the same four functions there as for loading content. Both calls can share the same functions or use their own.

Step 4: Define the display area

On the template that is to receive the form, create a display area with an ID.

Step 5: Create a WS-Ajax section

Wrap the form template with a WS-Ajax section whose name ends with the ID of the display area.
If the same form is also used without AJAX, wrap the section markers again with ST-Ajax. Also remember the button: without AJAX it must not carry the function call, otherwise the form would not be submitted at all.

Syntax of the functions

Both functions expect the same parameters, only the first one differs. When loading, it is the link; when submitting, it is the ID of the form. Pay attention to the quotation marks: startFunction and failureFunction are passed as function objects, that is, without quotation marks and without parentheses. successCallback and errorCallback, on the other hand, are passed as strings containing the complete function call including parentheses.

ws_AJAXloadTemplate

Reference: WS-Charset

ws_AJAXsendFormTemplate

Possible integrations with AJAX

The following content and features can be integrated via AJAX with the functions described. The respective pages show the integration using a concrete example, together with the function names used there.
  • Loading information pages via AJAX
  • Loading and submitting forms via AJAX
  • Quick login for existing customers