You are currently viewing a snapshot of www.mozilla.org taken on April 21, 2008. Most of this content is highly out of date (some pages haven't been updated since the project began in 1998) and exists for historical purposes only. If there are any pages on this archive site that you think should be added back to www.mozilla.org, please file a bug.


TOC PREV NEXT INDEX

Embedding Gecko API


nsIURL


The nsIURL interface provides convenience methods that further break down the path portion of nsIURI. It is scriptable.

Methods
getCommonBaseSpec

Takes a second URI and compares it to this URL. 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.

this.spec
aURItocompare.spec
result

http://mozilla.org/

http://www.mozilla.org/

""

http://foo.com/bar/

ftp://foo.com/bar/

""

http://foo.com:8080/

http://foo.com/bar/

""

ftp://user@foo.com/

ftp://user:pw@foo.com/

""

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/

http://foo.com/a.htm#i

http://foo.com/b.htm

http://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:///

Syntax:

AUTF8String nsIURL::getCommonBaseSpec(
	in nsIURI aURIToCompare) 

Parameters:

aURIToCompare: The URI to compare to this URL.

Returns:

A string representing the common base.
getRelativeSpec

Takes a URI and returns a substring of this if it can be made relative to the URI passed in. Filename, query, etc are always returned except when URIs are identical.

Syntax:

AUTF8String nsIURL::getRelativeSpec(
	in nsIURI aURIToCompare) 

Parameters:

aURIToCompare: The URI to compare to this URL.

Returns:

A substring representing the relative spec, if it exists. If it does not exist, the entire URI spec is returned. If the two URIs are identical, "" is returned.
Attributes
attribute AUTF8String nsIURL::filePath

Gets and sets a path including the directory and file portions of a URL.For example, the filePath of "http://foo/bar.html#baz" is "/foo/bar.html". Some characters may be escaped.

attribute AUTF8String nsIURL::param

Gets and sets the parameters specified after the ";" in the URL. Some characters may be escaped.

attribute AUTF8String nsIURL::query

Gets and sets the query portion (the part after the "?") of the URL. If there isn't one, an empty string is returned. Some characters may be escaped.

attribute AUTF8String nsIURL::ref

Gets and sets the reference portion (the part after the "#") of the URL. If there isn't one, an empty string is returned. Some characters may be escaped.

attribute AUTF8String nsIURL::directory

Gets and sets the directory portion of a URL. If the URL denotes a path to a directory and not a file, e.g. http://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 (i.e. this is a syntactic, not a semantic breakdown of the Path). Using this attribute is therefore not a fail-safe way to identify something as a file. But you can use it to get just the leading directory portion. Some characters may be escaped.

attribute AUTF8String nsIURL::fileName

Gets and sets the file name portion of a URL. If the URL denotes a path to a directory and not a file, e.g. http://foo/bar/, then the directory attribute accesses the complete /foo/bar/ portion, and the fileName is the empty string. Note that this is based solely on searching for the last trailing slash, so it should not be used to identify something definitively as a file. Some characters may be escaped.

attribute AUTF8String nsIURL::fileBaseName

Gets and sets the file basename portion of a filename in a URL. Some characters may be escaped.

attribute AUTF8String nsIURL::fileExtension

Returns the 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.


Written by:Ellen Evans | Comments, questions, complaints? Bug 143387
TOC PREV NEXT INDEX