nsIURL 编辑

netwerk/base/public/nsIURL.idlScriptable 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)
 http://host/directory/fileBaseName.fileExtension?query
 http://host/directory/fileBaseName.fileExtension#ref
 http://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("http://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

AttributeTypeDescription
directoryAUTF8StringDirectory portion of a URL. If the URL denotes a path to a directory and not a file, for example http://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.
fileBaseNameAUTF8StringFile basename portion of a filename in a url. Some characters may be escaped.
fileExtensionAUTF8StringFile 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.
fileNameAUTF8StringFile name portion of a URL. If the URL denotes a path to a directory and not a file, for example http://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.
filePathAUTF8StringPath including the directory and file portions of a URL. For example, the filePath of "http://host/foo/bar.html" is "/foo/bar.html". Some characters may be escaped.
paramAUTF8String

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.
queryAUTF8StringQuery portion (the part after the "?") of the URL. If there isn't one, an empty string is returned. Some characters may be escaped.
refAUTF8StringReference 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.specaURIToCompare.specresult
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/barftp://foo.com/
ftp://foo.com/bar/ftp://foo.com/bar/b.htmlftp://foo.com/bar/
http://foo.com/a.htmhttp://foo.com/b.htmhttp://foo.com/
ftp://foo.com/c.htm#iftp://foo.com/c.htmftp://foo.com/c.htm
file:///a/b/c.htmlfile:///d/e/c.htmlfile:///
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:48 次

字数:10349

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文