Tag Archives: Client JS

QuickTip: Get right path when autolaunching an XPage

If you are using relative urls to you resources you might have got the problem with dead images and resources that doesn’t load. The obvious choice is to add the full url to the images.

Snap181

The problem is often related to when you autolaunch an XPage on database open.

Notes Autolaunch Tab

But there is another way than adding the full url for the problem. You need to add a trailing slash to your path then everything will work as expected again. And using these few javascript lines added to your front page onload event or in a script on the page will fix the problem.

var h=window.location.href;
if(h.indexOf(".xsp")==-1){
if(h.substring(h.length,h.length−1)!="/"){
 window.location.href+="/"

}}

Load once functionality for client js script libs

If you have a custom control and this control can be added more that once on a page.
You will probably get problems if it relies on some clientside libraries.

There is at least three ways to this problem.

1. Don’t add the client lib to the Custom Control add it to the XPage instead
Is  this a good approach, no I don’t think so because you need to remember to add it, and also update it.

2. Add it in a theme globally
If you do it this way your library will always be added to all pages even if you do not need it.

3. Add it inside the Custom Control
This is the best approach in my opinion   

Just add a computed text to your custom control

Add this code to check a requestScope var if it has a value if not add script lib.

if(requestScope.loadonce==null){
requestScope.loadonce="Y"
return "<script type=\"text/javascript\" src=\"clientscript.js\"></script>"
}

 You could evolve this code by inserting it into the head tag but It will work.