URL Javascript Component Parsing with Window.Location

Mindwatering Incorporated

Author: Tripp W Black

Created: 05/03/2006 at 12:09 PM

 

Category:
Notes Developer Tips
JavaScript

The window.location object contains the componets of a URL.
Often I need to get either the host name or just the relative path information either to the current view or form I have open in the browser. Those are very simple. More complex is parsing the dbpath name from the relative path.

Host/Domain Name
To get just the hostname, use the hostname property of the location object.
e.g. location.hostname
For http://www.mindwatering.com/Folder/Dbname.nsf/WP/SomePage, this would return, www.mindwatering.com.

If you want the hostname and the port, user the host property of location.
e.g. location.host

Full Path
To get the full URL path, use the href property of the location object.
e.g. location.href
For http://www.mindwatering.com/Folder/Dbname.nsf/WP/SomePage, this would return, http://www.mindwatering.com/Folder/Dbname.nsf/WP/SomePage.

To get the relative full path (not including the host/domain name), use the pathname property.
e.g. location.pathname
For http://www.mindwatering.com/Folder/Dbname.nsf/WP/SomePage, this would return, /Folder/Dbname.nsf/WP/SomePage.

Database Path Only
To get just the current database path, you need to trim off the extra path info. The only issue is that for home/launch page of a database the end of the url might end in .nsf, so removing a slash would not work. The key is to use the .nsf as the end point. See below:
function GetFilePath() {
var url = window.location.pathname;
var urlcase = url.toLowerCase();
var nsfpos = urlcase.indexOf('.nsf');
filepath = url.substring(0, nsfpos + 4);
return filepath;
}

Reload Same Page
To go reload same page is:
var url = window.location.href;
window.location = url;

Get Relative URL (with ? or !)
To get the relative URL path including the folder(s), the app, and through any documents open (but not including query string parameters), see below:
function GetRelPath() {
var relpath = '';
var url = window.location.pathname;
var urlcase = url.toLowerCase();
var exclpos = urlcase.indexOf('!');
if (exclpos > -1) {
relpath = url.substring(0, exclpos);
} else {
relpath = url;
}
return relpath;
}







previous page