Monday, June 6, 2011

Simple helper function to get path to an application

I use this when generating URLs in SSJS. The reason that protocol/hostname is added is so that the generated urls work with xp:link.

If an xp:link url starts with /, it generates the path to the current application at the start of the url. If you want to link to another app/page on the current server you either have to make an html link (<a>), hard code the path, or use something like this function.

// Returns absolute path (including protocol/hostname) to the current application
// or specified [database]
function getAppPath( db:NotesDatabase ){
try {
var currentUrl = context.getUrl();
var hostname = currentUrl.getHost();
var protocol = currentUrl.getScheme();

db = db || database;
return protocol + '://' + hostname + '/' + db.getFilePath().replace( /\\/g, '/' );
} catch( e ){ /* Exception handling */ }
}

1 comments:

Anonymous said...

Please note that sometimes server needs to use other port than default(80). Therefore it's good to check that also:
port = (request.getServerPort().equals(80)) ? "" : ":" + request.getServerPort();