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. 

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>