Search
Contribute
Navigation
- 8.5
- 8.5
- action bar
- agents
- ajax
- app dev
- application
- Beginner
- Best Practices
- bidi
- Browser
- Build
- Caching
- capitalization
- CGI variables
- Checkbox Group
- Classic Web Server
- Classic Web Server
- client
- code snippets
- command buttons
- components
- composite applications
- Container Controls
- context
- context menu
- controls
- converters
- Cookie
- createForm
- css
- custom
- Data binding
- Data Palette
- Data Table
- database
- datetime
- DAV
- db2
- debugging
- design elements
- Designer
- developing
- dialog boxes
- Display Error Control
- Display Errors Control
- Dojo
- Dojo
- Domino
- Domino Designer
- Domino Designer 8.5
- DXL
- Eclipse
- ECMAScript
- ellipses
- error handling
- errors
- events
- Expeditor
- ExternalContext
- FacesContext
- FAQ
- fields design
- forms
- formulas
- getting started
- globalization
- glow
- handler
- hover help
- HTML
- http
- hybrid
- icons
- input validation
- interaction
- interface
- internationalization
- ISV XPages View Icon
- Java
- JavaScript
- JSF
- JSON
- Key bindings
- layout
- left-to-right
- letterhead
- lighting
- localization
- logging
- Lotus Script
- Lotus Script Query_String
- Lotusphere 2009
- LotusScript
- ltr
- mask
- memory
- menu bar
- messages
- mnemonics
- navigation
- navigator
- new user
- Notes 8
- NotesXspDocument
- notes.ini
- ntf
- numbers
- open button
- Open Source
- OpenNTF
- outline
- panels
- Partial Update
- pattern matching
- performance
- Portal
- preferences
- programming
- punctuation
- queryview
- Radio Button Group
- radio buttons
- Redbooks wiki
- regex
- regular expressions
- repeat control
- Repeats
- replica
- rich client
- Rich Text
- right-to-left
- roadmap
- Robot
- Rohit
- RSS
- rtl
- Sametime
- sample code
- Scope
- search
- security
- server
- shadow
- Shortcuts
- SHOW106
- simple actions
- size
- skipContainers
- SOA
- soft deletions
- states
- stationary
- status bar
- subforms
- tabs
- templates
- test
- title bars
- titles
- toolbar
- tooltips
- troubleshooting
- validation
- Variables
- Video
- View
- View Control
- ViewPanel
- views
- Web
- Web Server
- Web services
- working set
- XHTML
- XML
- xpage
- XPages
- XPages
- XSPClientDojo
- $$ViewTemplate
- @Formulas
Go elsewhere
Are global variables in server-side JavaScript libraries shared across sessions in XPages?
Article information
![]() |
FAQ
, XPages
8.5 , JavaScript , XPages |
Thomas Gumz 11/04/2008 |
Thomas Gumz 11/05/2008 |
|
Question:
Are global variables in server-side JavaScript libraries shared across the board regardless of the user/session ?
Answer:
Yes, global variables defined in server-side JavaScript libraries are initially indeed globally shared across sessions.
However, they are not guaranteed to be available for the life-cycle of the application and might get discarded by the run-time at any time.
Therefore, it is not recommended to rely on this.
To understand the reasons for this, let's look at how server-side script libraries work:
A script library is loaded once a page requests it. It is compiled, the statements outside a function body are executed, and a list of global variables is maintained.
For example, each function defined in the library is a global variable of type "function", as well as the constants you defined.
How long this data is kept in memory is undefined. If the operating system is reclaiming memory, then this data is discarded, and will be reconstructed when a page requests it again. After it has been discarded and reconstructed, the original value of the global variables are lost.
Note that data cannot be discarded while they are actively in use by a page, so it is safe during the request phase for a page.
The recommended way to reliably store and share global variables across user sessions is to use the applicationScope.
To illustrate this, consider the following sample XPage:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.resources>
<xp:script src="/global.jss" clientSide="false"></xp:script>
</xp:this.resources>
<xp:this.afterPageLoad>
<![CDATA[#{javascript:addToCounter(5); print("Updated counter value: " + applicationScope.counter);}]]>
</xp:this.afterPageLoad>
</xp:view>
and this code in the associated server-side "global.jss" JavaScript library:
applicationScope.counter = 0;
print("Initial counter value: " + applicationScope.counter);
function addToCounter(value) {
applicationScope.put("counter", applicationScope.counter + value);
}
When the page gets loaded the very first time, the server console output will be:
HTTP JVM: Initial counter value: 0
HTTP JVM: Updated counter value: 5
Any additional page loads from different clients will continue to increase the global counter value by 5 each time the page gets loaded:
HTTP JVM: Updated counter value: 10
HTTP JVM: Updated counter value: 15
HTTP JVM: Updated counter value: 20
HTTP JVM: Updated counter value: 25
Comments



