Client-side scripting
The triggering of a Client event sends its JavaScript code plus the XPage context to an interpreter on the user's client.
- Has access to the client Document Object Model for XML and HTML.
- Does not have access to server JavaScript libraries for accessing the document store and performing other activities.
- Cannot be used for formulas.
- Has access to client script libraries only.
- Uses the standard JavaScript found in the user's client.
Getting server information
You can pass information from the server to the client by embedding a formula in XSP format in the client-side script. The syntax for JavaScript is as follows:#{javascript:statements}
window.alert("#{javascript:session.getCommonUserName()}")
Getting the identifier of a DOM element
The run time assigns identifiers to DOM elements based on the names of the design controls. For example, the run time identifier for inputText1 might be view:_id1:inputText1. The identifer is not predictable so should not be specified as a literal.#{id:control_name}
var e = window.document.getElementById("#{id:inputText1}");
e.value = e.value.toUpperCase()
Getting event information
You can use the identifier thisEvent to acquire the Event object for the event in which the client code is running. This gives you access to the standard Event properties.var e = window.document.getElementById(thisEvent.target.id);
if (e.value == "") {
e.value = "enter valid name"
}
Attaching two scripts to an event
You can attach a client-side and server-side script to the same event:- The client-side script executes first before the service request for the event goes to the server.
- The server-side script executes next upon receipt of the service request, unless the client-side script returns false. A return value of false cancels the service request.
Alternatively you can attach simple actions or simple action groups. You can attach a script to one side (client or server) of the event and a simple action or simple action group to the other side. You can attach multiple scripts to an event by using the Execute Script or Execute Client Script simple action in a simple action group. See Simple actions.
if(window.confirm("Do you want to continue?") != true)
return false
Triggering a client script from a server script
The UI view root, accessible through the view global object, has a method postScript that executes a script after the server responds to the client. Suppose a button onclick event has a server event with two simple actions. The first simple action is Save Document while the second simple action is Execute Script with the script specified as follows:view.postScript("alert('document saved')")
When the user clicks the button, the server-side event saves the XPage data to a document then responds to the client at which time the alert dialog appears.
Detecting errors
Client-side errors are not reported at design time. The editor might highlight code that is incorrect, but the Problems view does not report the error and the deployment process does not report the error. At run time erroneous code may be reported but may fail silently.window.alert("hello client world)
To detect client-side errors, turn on the browser's error, JavaScript, or Java™ console. This typically brings up another window that reports errors. For example, in Mozilla Firefox, click For the example case, the window that opens displays unterminated string literal.