这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
URL 接口是一个包含若干静态方法的对象,用来创建 URLs。
当使用一个没有实现该构造器的用户代理时,可以通过 Window.URL 属性来访问该对象(基于 Webkit 和 Blink 内核的浏览器均可用 Window.webkitURL 代替)。
Note: 此特性在 Web Worker 中可用。
属性
实现 URLUtils 中定义的属性。
URLUtils.href- 包含完整 URL 的
DOMString。 URLUtils.protocol- 包含 URL 协议名的
DOMString,末尾带':'。 URLUtils.host- 包含 URL 域名,
':',和端口号的DOMString。 URLUtils.hostname- 包含 URL 域名的
DOMString。 URLUtils.port- 包含 URL 端口号的
DOMString。 URLUtils.pathname- 以 '/' 起头紧跟着 URL 文件路径的
DOMString。 URLUtils.search- 以 '?' 起头紧跟着 URL 请求参数的
DOMString。 URLUtils.hash- 以 '#' 起头紧跟着 URL 锚点标记的
DOMString。 URLUtils.username- 包含在域名前面指定的用户名的
DOMString。 URLUtils.password- 包含在域名前面指定的密码的
DOMString。 URLUtils.origin只读- 返回一个包含协议名、域名和端口号的
DOMString。
URLUtils.searchParams- 返回一个用来访问当前 URL GET 请求参数的
URLSearchParams对象。
构造器
URL()- 使用给定参数创建并返回一个
URL对象。
方法
URL 接口实现的在 URLUtils 中定义的方法。
URLUtils.toString()- 返回一个包含完整 URL 的
DOMString。它是URLUtils.href的别名,但区别在于 toString 不能用于修改值。
静态方法
URL.createObjectURL()- 返回一个
DOMString,包含一个唯一的blob链接(该链接协议为以blob:,后跟唯一标识浏览器中的对象的掩码)。 URL.revokeObjectURL()- 销毁之前使用
URL.createObjectURL()方法创建的URL实例。
规范
| Specification | Status | Comment |
|---|---|---|
| File API URL |
Working Draft | Added the static methods URL.createObjectURL() and URL.revokeObjectURL(). |
| URL Node |
Living Standard | Initial definition (implements URLUtils). |
浏览器兼容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 8.0 as webkitURL35 |
4.0 (2.0) (non-standard name) [1] 19.0 (19.0) |
10.0 | 15.0 as webkitURL |
6.0 as webkitURL7.0 |
URLUtils properties |
32 | (Yes) | ? | 19 | (Yes) |
username, password, and origin |
32 | 26.0 (26.0) | ? | 19 | (Yes) |
protocol, host, hostname, port, href, pathname, search, hash |
32 | 22.0 (22.0) | ? | 19 | (Yes) |
searchParams |
32 | 29.0 (29.0) | ? | 19 | (Yes) |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | (Yes)as webkitURL |
14.0 (14.0)(non-standard name) [1] 19.0 (19.0) |
(Yes) | 15.0 as webkitURL |
6.0 as
|
URLUtils |
? | 未实现 | ? | ? | (Yes) |
username, password, href, and origin |
? | 26.0 (26.0) | ? | ? | (Yes) |
[1] From Gecko 2 (Firefox 4) to Gecko 18 included, Gecko supported this interface with the non-standard nsIDOMMozURLProperty internal type. As the only to access such an object was through window.URL, in practice, this didn't make any difference.
Chrome 浏览器无效
为了在chrome中使用, JSM and Bootstrap scope, 你必须像下面这样引入:
Cu.importGlobalProperties(['URL']);
URL可在Worker下使用。
相关链接
- Property allowing to get such an object:
Window.URL. - Components.utils.importGlobalProperties