Using scripts

You can use device specific scripts in pages that are device independent, providing different versions for specific devices or device families. You use a script policy to define the script for a device. MCS selects the appropriate version of the script to use based on the characteristics of the device accessing the page.

You can use device specific scripts in pages that are device independent, providing different versions for specific devices or device families. You use a script policy to define the script for a device. MCS selects the appropriate version of the script to use based on the characteristics of the device accessing the page.

Script variants

You use the Text option in the Variant Metadata section of the policy editor to define whether a script variant can contain the script code itself, or the URL of the script code. If you check the control then the URL and Asset group control are disabled, and you can enter the script text. Otherwise the other controls are enabled, and the variant contains the URL of the script.

The required Encoding attribute defines the programming language of the script. For example, MCS generates this value in the language attribute of the HTML script.

Optionally, you can use the Character set attribute to define the character set in which the script is written. If specified, MCS uses the value in the charset attribute on the HTML script element.

MCS locates the most appropriate variant by traversing the tree of device-dependent variants in just the same way as it does for text or image variants. If there is no script variant available for the specific device, the search proceeds through the parents of that device until a variant is found.

Script policies are unusual in that they do not support fallback to text. However, you can provide alternate content by using the noscript element.

Associating scripts with events

MCS invokes a script in response to an event, such as a mouse action or key press. So you can refer to scripts from any of the event attributes used in XDIME. For example, you might define an onmousedown attribute to point to your script policy with an inline a element. The script will display information about the source document before the user releases the mouse button.

<a onclick="{/showmetadata.mscr}">Administering MCS</a>

Note however, that inline scripts must be specified differently:

<a onclick="\{alert('welcome')\}"> click me </a>

The script element

Referring to script variants from events is a useful feature for very simple scripts, but suppose that you want to load a script containing several functions and refer directly to the functions from event attributes. In this case you can define a script reference with the script element.

<script
  id="ro1"
  src="{/ReleaseOpts.mscr}"
  defer="true"/>

You use the src attribute to refer to the script policy to use, or to refer directly to a script file using a URL. If it contains an MCS expression in the form {[policy_name]} MCS uses the named script policy. If this attribute contains literal text, MCS looks for a script file.

In the example the policy named 'ReleaseOpts' is referenced. The id attribute is the unique identifier for the script. The defer attribute specifies that the browser can defer interpretation of the script to improve initial page loading performance.

A script variant associated with this policy contains a JavaScript function, function setReleaseOptions(intprd,type).

Now you can call the function directly from a single select element on a form.

<xfform class="myform" name="task">
  <xfsiselect
    styleClass="myform" name="product"
    onchange="setReleaseOptions(task.product.selectedIndex,1)">
  </xfsiselect>
</xfform>

The noscript element

To provide alternative markup for use when a script is not available you can use the optional noscript. MCS uses the idref attribute value to locate a script element with a matching id attribute. The replacement text goes in the element content.

If the device can handle JavaScript, MCS wraps the content with the HTML noscript tags.

MCS will also render noscript content in the following cases:

Note:

If you use noscript, it must appear after the script element to which it refers.

<?xml version="1.0" encoding="UTF-8"?>
<canvas layoutName="/welcome.mlyt" pageTitle="noscript">
  <pane name="background">
    <h3>Welcome</h3>
    <script id="scripttest" src="{/script123.mscr}"/>
    <noscript idref="scripttest">
      <p>We're sorry, but something went wrong!</p>
    </noscript>
  </pane>
</canvas>

Related topics