Opening database relative URLs in JavaScript

It is a little known fact the domino database structure behaves in much the same way as a standard directory strucure. Using this fact we can navigate through a database without having to include the whole path in the link as I discussed in this article. However, it is not always possible to predict at what level of the DB structure the user is at. For instance, when a link is in both a document form and a view template form. That is why it is sometimes better to use the whole path of the database when generating links.

The usual method to make links that include the database path is to create a Computed For Display text field called something like "DBPath" that has the following formula:

@ReplaceSubstring(@Subset(@DBName; -1); "\\"; "/");

You can then refer to this value from any formula or if you use this method. It is even possible to get the value using JavaScript. For example, a Computed Text area on a document that creates a reply:

"<a href=\"/" + DBPath + "/reply?OpenForm&ParentUnid=" + @Text(@UniqueID) + "\">Reply</a>"

or in JavaScript if you wanted to return to a view from a document then the onclick event of the button would be:

location.href = ("/" + document.forms[0].DBPath.value + "/AllDocs?OpenView");

Well what about using JavaScript and not having to use the DBPath field at all. This way we can make it generic and easy. It will even work on the Page design element where we can't use fields. Take a look at the following function:

function openDBRelativeURL( url, target ){
//Check we have a target window;
target = (target == null ) ? window : target;
//Work out the path of the database;
path = location.pathname.split('.nsf')[0] + '.nsf/';
target.location.href = path + url;
The important part of this function is on the line beginning "path =". This is what replaces the DBPath field. It extracts everything after the server/host and up to and including the ".nsf", which we assume indicates the end of the DB's path. Click here to see if this will work in the browser you are using now (should do in all!!)

The best place for a function like this is in a JavaScript source file/page (roll on Rnext!!) called "common.js". This page is then linked in to every form/page in the database to ensure it is always available. Alternatively you could add it to the JS Header of a Common Fields subform.

Now that we have this function available the first formula I showed you would become:

"<a href=\"JavaScript :openDBRelativeURL(\'reply?OpenForm&ParentUnid=" + @Text(@UniqueID) + "\');">Reply</a>"

or the JavaScript that I showed you to return to a view could simply be:


Notice that, in the function, there is an optional second argument called "target". We can use this to point the link at a certain frame (Leaving the argument out the function assumes you mean the current window). Let's suppose we want the link to open a frameset in the top-most frame ("_top"), we could use this link:

openDBRelativeURL('Home?OpenFrameset', window.top);

or to open a page in a frame called "main"

openDBRelativeURL('Home?OpenPage', window.main);

If you have got this far you may well be wondering "what is the point?". Good question! No real answer to that. This is just one of those things I like to do to make my life a little easier. I don't need to worry anymore about whether the "DBPath" field exists or what it is called - I just call this function.

Note: This method is not perfect. The reason I don't like to use it too often or in anything other than the onclick event is that it breaks some inherent browser functionality. Used in the onclick of a button is fine but when used as the "href" of a link we can no longer use the "Open in new window" feature. Consider the link below. This uses the function to return you to all documents by date view:

openDBRelativeURL( 'all?OpenView', this);

Try and right click on this and use the "Open in New Window" command or simply Shift-Click it (IE). It don't work does it! Not a problem as such, but something that I find frustating when browsing a site (like this one) where it is nice to be able to open a document and leave it to load while you carry on reading the current page. Make your own mind up where and when to use this....


  1. A couple of "Gotchas"

    First, when I tried to implement the function I ran into the problem that some files end with ".NSF", not ".nsf". Say, for example, my pathname is "/bob/giggy.NSF: and the url passed in is "pagetoopen", I'd get "/bob/giggy.NSF/pagetoopen.nsf" when using:

    path = location.pathname.split('.nsf')[0] + '.nsf/';

    I don't know whether or not there are ucase and lcase equivalents in JavaScript (I am curious). My solution was to replace the line above with:

    path = location.pathname.slice( 1, location.pathname.lastIndexOf("/"))

    Second, beware of the the "default target for links in frame" when working with framesets. I called the function from within anchor tags on a page that served as the source of the "left" frame. I wanted all of the links to open in the "right" frame. No matter how I wrote the "target" variable, the function would not work until I turned off the "default target" property for the "left" frame (it had been set to "right").

    Just a couple of things caveats. It's a great function that has enabled me to replace forms that I had been using with pages.



    1. Re: A couple of "Gotchas"

      Good points David. And to think I call myself a developer....

      There is a lcase equivalent in JS. It would be:

      path = location.pathname.toLowerCase().split('.nsf')[0] + '.nsf/';

      This would present another gotcha however, in that, if the DB lives on a *nix server then the file name is case-sensitive and this would lead to a 404 error.

      There is also a problem in your workaround, where you look for the last "/". If you are at the root level of a database. i.e the homepage and the location is "/dir/db.nsf" the file name returned would be "/dir/". So what's the perfect solution to this then I wonder? I think I'll stick to naming all my DBs in lower case.

      Thanks for the feedback.....

      Jake -CodeStore

      Show the rest of this thread

Your Comments


Navigate other articles in the category "JavaScript"

« Previous Article Next Article »
Debugging JavaScript in Netscape   Debugging JavaScript in Your Applications

About This Article

Author: Jake Howlett
Category: JavaScript
Keywords: url; path; open;


Print Friendly

Let's Get Social

About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »