Subscribe via Feed

Two Helpful Functions in the XSP Clientside Javascript Object

Jeremy Hodge, Mar 25, 2010 12:50:18 PM

There are two helpful (among others) functions in the client-side XSP Javascript Object:

XSP.startsWith(string, substring)

XSP.endsWith(string, substring)

They are string functions that return true/false if the passed in string starts or ends with substring.  Very helpful when coordinating client-side IDs to Server side IDs, for example, inside event handlers.

As an example, You can connect 1 event handler to two objects, that basically do the exact same thing, but may have slight variations for one object or the other. Take objectA and objectB; objectA is a button with the XPage ID 'niceGuy' and objectB is a button with an XPage ID of 'badGuy' and do (client-side):

dojo.connect(objectA, 'onclick', myHandler);
dojo.connect(objectB, 'onclick', myHandler);

var myHandler = function(e) {

  e = (e) ? e : window.event;
  alert('You clicked me! How dare you. The nerve of some people, i swear.');

  if (XSP.endsWith(e.target.id, 'niceGuy'))
    alert('Just Kidding, you seem like a nice ol' chap.')
  else
    alert('Shove off...');

}

While doing the check on your own, with your own code isn't all that difficult, you'd have to ask yourself why? Its already there for you!

Happy Coding!



1 responses to Two Helpful Functions in the XSP Clientside Javascript Object

S.Csaba, April 28, 2010 6:03 AM

I don't see why this function is better, than String.indexOf() ???

Ok, it is a bit comfortable... But I would rather use basic javascript functions in client side...