Opening database relative URLs in JavaScript
Jake Howlett, 12 April 2001
Category: JavaScript; Keywords: url
path
open
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:
openDBRelativeURL('AllDocs?OpenView');
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....
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net