Ok so when you create your fields or divs in XPage and you give them a sane Id, when XPage then renders them you get ids like:
id=”view:_id1:_id2:ContentControl:CompanyName”
Please do notice that this applies only to script code which is loaded into the XPage. I’m well aware of that JavaScript entered directly into the Xpage or custom control will not have the problem, since there are many diffrent way to reference the rebuilt XPage-id.
I do see why it is necessary, since you may have multiple forms in one XPage and it would be impossible to keep them apart if fields did not have unique names.
But what if you have your old JavaScript libraries which uses functions like getElementById or if you are smarter than that and uses the dojo.byId() which is browser independent.
There is no way using the client side syntax like #{id:comboBox} in JavaScript libraries. Works fine in XPages and custom controls though.
At the moment I see two approaches, the first is to create a global variable which is then used in the JavaScript library. This method is described here!
The second method is to use dojo.query()!
dojo.query can be used to “query” the DOM tree, a really powerful function which is well worth digging into.
But in this case we will use it to find our html element by querying for the elements where XPage-id ends with the id we are looking for.
So the syntax is dojo.query(“[id$=’:CompanyName’]”).
Let’s take it apart. The brackets means we are searching for a tag with the name “id” ([id$=’…), which value ends with our search string, ‘$=’ stands for ends with.
The search string begins with a colon since we want to be sure we do not get fields with ids like ‘FirstCompanyName’, since they also ends with ‘CompanyName’.
The result will be an array of html elements, with multiple entries, which may be the case!
I’ll try to explain why and when to use which?
The first method is preferred if you have multiple forms as data sources for your fields in the XPage or when you cannot be absolutely sure that the ids are unique over all elements used in your JavaScript library!
In all other cases the second method should work fine!
So now you have to rewrite all your JavaScript libraries! Yes you do! And when you do that try to skip everything which takes an id string as a parameter, rewrite it to use elements or better accepts both types as an argument.
Function doSomthingWithElement(elmnt){ if(typeof(elmnt) === ‘string’){ elmnt = dojo.byId(elmnt); } // use the element alert(elmnt.innerHTML); }
Now with a little twist of customization!
What you also can do is to replace the XSP objects method getElementById with your own implantation of it, using the dojo.query().
Either put the code you find as parameter to dojo.addOnLoad in the client side XPage event onClientLoad or better put the code below in to a JavaScript library where you have your common functions, then add it as a resource into the XPage.
The XSP object is created by Domino server and contains different convenient functions like getElementsById which actually is a wrapper for dojo.byId()!
dojo.addOnLoad(function(){ dojo.mixin(XSP, { getElementById: function(idName){ var result = dojo.byId(idName); if (!result){ result = dojo.query("[id$=':" + idName + "']"); if (result){ result = result[0]; } } return result; } }) });
I’ll try to explain what happens.
First the dojo.addOnLoad() will run the function given as a parameter when the page has loaded in the client. Then by using dojos mixin the script replaces the object literal entry for “getElementById” with the code given as a second parameter. The code itself doing the magic first tries to find the element the classic way by using dojo.byId(). In this way we do not break the functionality. If no elements are found it tries to find the element by using the dojo.query() as described above.
So now if you want to find an element where the id has been replaced by the Domino XPage rendering engine you can use XSP.getElementById(‘CompanyName’).
It’s also possible to add it to the dojo object by altering the code to:
dojo.mixin(dojo, { xspById: function(idName){
I’ve did not manage to replace the dojo.byId() since it references it self in a “strange” way which is broken when you use dojo.mixin
If someone knows of a function which can be used instead in custom JavaScript libraries please comment!