Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

nsIURL

This interface provides convenience methods that further break down the path portion of nsIURI.
Inherits from: nsIURI Last changed in Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6)

 https://host/directory/fileBaseName.fileExtension?query
 https://host/directory/fileBaseName.fileExtension#ref
 https://host/directory/fileBaseName.fileExtension;param
            \          \                       /
             \          -----------------------
              \                   |          /
               \               fileName     /
                ----------------------------
                            |
                        filePath

You can get a nsIURL from an nsIURI, using the QueryInterface() method:

var myURI = Components.classes["@mozilla.org/network/io-service;1"]
                      .getService(Components.interfaces.nsIIOService)
                      .newURI("https://developer.mozilla.org", null, null);
try {
  var myURL = myURI.QueryInterface(Components.interfaces.nsIURL);
}
catch(e) {
  // the URI is not an URL
}

Or using instanceof:

if (myURI instanceof Components.interfaces.nsIURL) {
  // Your code here
}

 

Method overview

AUTF8String getCommonBaseSpec(in nsIURI aURIToCompare);
AUTF8String getRelativeSpec(in nsIURI aURIToCompare);

Attributes

Attribute Type Description
directory AUTF8String Directory portion of a URL. If the URL denotes a path to a directory and not a file, for example https://host/foo/bar/, then the Directory attribute accesses the complete /foo/bar/ portion, and the FileName is the empty string. If the trailing slash is omitted, then the Directory is /foo/ and the file is bar (that is this is a syntactic, not a semantic breakdown of the Path). And hence don't rely on this for something to be a definitely be a file. But you can get just the leading directory portion for sure. Some characters may be escaped.
fileBaseName AUTF8String File basename portion of a filename in a url. Some characters may be escaped.
fileExtension AUTF8String File extension portion of a filename in a url. If a file extension does not exist, the empty string is returned. Some characters may be escaped.
fileName AUTF8String File name portion of a URL. If the URL denotes a path to a directory and not a file, for example https://host/foo/bar/, then the Directory attribute accesses the complete /foo/bar/ portion, and the FileName is the empty string. Note that this is purely based on searching for the last trailing slash. And hence don't rely on this to be a definite file. Some characters may be escaped.
filePath AUTF8String Path including the directory and file portions of a URL. For example, the filePath of "https://host/foo/bar.html#baz" is "/foo/bar.html". Some characters may be escaped.
param AUTF8String

Parameters specified after the ; in the URL. Some characters may be escaped. Obsolete since Gecko 9.0

Note: This was removed in Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) because the semicolon is not actually valid for this purpose and should not have been specially handled.
query AUTF8String Query portion (the part after the "?") of the URL. If there isn't one, an empty string is returned. Some characters may be escaped.
ref AUTF8String Reference portion (the part after the "#") of the URL. If there isn't one, an empty string is returned. Some characters may be escaped.

Methods

getCommonBaseSpec()

This method takes a uri and compares the two. The common uri portion is returned as a string. The minimum common uri portion is the protocol, and any of these if present: login, password, host and port If no commonality is found, "" is returned. If they are identical, the whole path with file/ref/etc. is returned. For file uris, it is expected that the common spec would be at least "file:///" since '/' is a shared common root.

Examples:

this.spec aURIToCompare.spec result
https://mozilla.org/ https://www.mozilla.org/ ""
https://foo.com/bar/ ftp://foo.com/bar/ ""
https://foo.com:8080/ https://foo.com/bar/ ""
ftp://[email protected]/ ftp://user:[email protected]/ ""
ftp://foo.com/bar/ ftp://foo.com/bar ftp://foo.com/
ftp://foo.com/bar/ ftp://foo.com/bar/b.html ftp://foo.com/bar/
https://foo.com/a.htm#i https://foo.com/b.htm https://foo.com/
ftp://foo.com/c.htm#i ftp://foo.com/c.htm ftp://foo.com/c.htm
file:///a/b/c.html file:///d/e/c.html file:///
AUTF8String getCommonBaseSpec(
  in nsIURI aURIToCompare
);
Parameters
aURIToCompare
A URI to compare with
Return value

The common URI portion

getRelativeSpec()

This method takes a URI and returns a substring of this if it can be made relative to the uri passed in. If no commonality is found, the entire uri spec is returned. If they are identical, "" is returned. Filename, query, and so forth are always returned except when uris are identical.

AUTF8String getRelativeSpec(
  in nsIURI aURIToCompare
);
Parameters
aURIToCompare
A URI to compare with
Return value

The common URI portion

See also

Document Tags and Contributors

 Contributors to this page: def00111, Sheppy, trevorh, Smi, Grubshka
 Last updated by: def00111,