JavaScript libraries

A JavaScript™ library allows you to store classes, functions, and variables for common use within an application. A JavaScript library is either a client library or a server library.

Script libraries reside under Code in the Applications Navigator. You edit a JavaScript library like any JavaScript code element. However a JavaScript library typically contains functions for use in other code elements. Here is a server library named sl1 that contains one function:
// Create document with subject item
// XPage must have edit box bound to requestScope.subject to get value of subject
// XPage must have computed field bound to requestScope.status
function cdoc() {
	var doc = database.createDocument();
	doc.replaceItemValue("subject", requestScope.subject);
	doc.save();
	requestScope.status = "document created"
}
Here is an XPage with a button that imports and calls the library function:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:inputText id="inputText1" value="#{requestScope.subject}"></xp:inputText><xp:br></xp:br>
<xp:button value="create" id="button1">
	<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
		<xp:this.action><![CDATA[#{javascript:import sl1;
		cdoc()}]]></xp:this.action>
	</xp:eventHandler>
</xp:button>	<xp:br></xp:br>
<xp:text escape="true" id="computedField1" value="#{requestScope.status}"></xp:text>
</xp:view>

The import statement is not necessary if you make the library a resource on the XPage. With focus outside all controls, click on the Resources tab under Properties. Then use the Add button to make the library available as a resource.

Use the Resources tab to add client libraries to an XPage. The import statement is not available in client JavaScript.