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

URL()

这篇翻译不完整。请帮忙从英语翻译这篇文章

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀.由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变.

The URL() constructor returns a newly created URL object representing the URL defined by the parameters.

If the give base URL or the resulting URL are not valid URLs, a DOMException of type SYNTAX_ERROR is thrown.

Syntax

url = new URL(urlString, [baseURLstring])
url = new URL(urlString, baseURLobject)

Parameters

urlString
Is a DOMString representing an absolute or relative URL. If urlString is a relative URL, baseURLstring or baseURLobject, whichever is present, will be used as the base URL. If urlString is an absolute URL, baseURLstring and baseURLobject are ignored.
baseURLstring 可选
Is a DOMString representing the base URL to use in case urlString is a relative URL. If not specified, and no baseURLobject is passed in parameters, it default to 'about:blank'. If it is an invalid absolute URL, the constructor will raise a DOMException of type SYNTAX_ERROR
baseURLobject
Is a URL object representing the base URL to use in case urlString is a relative URL.

Example

var a = new URL("/", "https://developer.mozilla.org"); // Creates a URL pointing to 'https://developer.mozilla.org/'
var b = new URL("https://developer.mozilla.org");      // Creates a URL pointing to 'https://developer.mozilla.org'
var c = new URL('en-US/docs', b);                      // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var d = new URL('/en-US/docs', b);                     // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var f = new URL('/en-US/docs', d);                     // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var g = new URL('/en-US/docs', "https://developer.mozilla.org/fr-FR/toto");
                                                       // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var h = new URL('/en-US/docs', a);                     // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var i = new URL('/en-US/docs', '');                    // Raises a SYNTAX ERROR exception as '/en-US/docs' is not valid
var j = new URL('/en-US/docs');                        // Raises a SYNTAX ERROR exception as 'about:blank/en-US/docs' is not valid
var k = new URL('https://www.example.com', 'https://developers.mozilla.com');
                                                       // Creates a URL pointing to 'https://developer.mozilla.org'
var l = new URL('https://www.example.com', b);          // Creates a URL pointing to 'https://www.example.com'

Specification

Specification Status Comment
URL
URL.URL()
Living Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 26.0 (26.0) 未实现 ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? 26.0 (26.0) 未实现 ? ?

See also

  • The interface it belongs to: URL.

 

文档标签和贡献者

 此页面的贡献者: teoli, leozdgao, khalid32, iori
 最后编辑者: leozdgao,