Skip to main content

Posts

Showing posts from 2012

Quicktip: Elements need IDs to attach client-side events

I ran across something today that took me a few minutes to figure out, so I thought I'd share it. I was trying to make a column heading toggle the display of the column below when clicked. So I wrote up a quick function like this: function toggleDivDisplay(divId) { // call with toggleDivDisplay('#{javascript:getClientId("**XspIdHere**")}'); var div = document.getElementById(divId); if (div.style.display == "") { div.style.display = "none"; } else { div.style.display = ""; } } This function was called from the onclick event for the heading Div I had created, and everything worked swimmingly. However when I used the same technique on a different column, it failed. I spent a few minutes making sure the target ID matched what I was sending, and sure enough it did, and I was left scratching my head. That was when I realized how client-side JS is actually attached: it isn't added to the HTML element itself. A

Move all your code into script libraries

Here is a little tip and the reason behind it: If you have any code on an XPage, replace it with a single function call and move the code to a script library. Example: If you have code like this on your XPage <xp:this.querySaveDocument><![CDATA[#{javascript: var foo = "hi"; var bar="there"; return (foo==bar?True:False);}]]></xp:this.querySaveDocument> Replace it with this <xp:this.querySaveDocument><![CDATA[#{javascript: return foobarQuerySave();}]]></xp:this.querySaveDocument> And create and include a script library with this function foobarQuerySave() {    var foo = "hi";    var bar = "there";    return (foo==bar?True:False); } Even if there is no code there currently, if you think there may ever be a need for it you should add a stub function that just returns true and call it. Here's why:

Setting a field attribute through javascript

Quick little post here about something that came up on a project. If you need to set an attribute of a field that the inputText bean does not have the attribute for, such as the TYPE attribute prior to 8.5.3, you can do so using javascript. Follow the field with a little script block that looks like this: <xp:scriptBlock value="document.getElementById('#{id:StartDate}').setAttribute('type', 'date');"> </xp:scriptBlock>

Pass data between XPages and existing LS scripts

I'm working on modernizing a fairly hefty application with a lot of existing script libraries which we want to leverage within the XPages environment. Here is a technique that works very well. First, create an in-memory document in SSJS. We can set any input values needed for the back end. Then we pass that document to a LS Agent which can work it's magic using the values set in SSJS and use the same document to return values back to the XPage. Here is how it works in detail: