Skip to content

Commit dfdd052

Browse files
committed
docs(url): add URL.parse()
1 parent c07d5cb commit dfdd052

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

docs/url.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function handleFiles(files) {
173173

174174
### URL.canParse()
175175

176-
`URL.canParse()`用来检测一个字符串是否为有效 URL,它返回一个布尔值。
176+
`URL()`构造函数解析非法网址时,会抛出错误,必须用`try...catch`代码块处理,这样终究不是非常方便。因此,URL 对象又引入了`URL.canParse()`方法,它返回一个布尔值,表示当前字符串是否为有效网址
177177

178178
```javascipt
179179
URL.canParse(url)
@@ -217,6 +217,20 @@ function isUrlValid(string) {
217217

218218
上面示例中,给出了`URL.canParse()`的替代实现`isUrlValid()`
219219

220+
### URL.parse()
221+
222+
`URL.parse()`是一个新添加的方法,Chromium 126 和 Firefox 126 开始支持。
223+
224+
它的主要目的就是,改变`URL()`构造函数解析非法网址抛错的问题。这个新方法不会抛错,如果参数是有效网址,则返回 URL 实例对象,否则返回`null`
225+
226+
```javascript
227+
const urlstring = "this is not a URL";
228+
229+
const not_a_url = URL.parse(urlstring); // null
230+
```
231+
232+
上面示例中,`URL.parse()`的参数不是有效网址,所以返回`null`
233+
220234
## 实例方法
221235

222236
### toString()

0 commit comments

Comments
 (0)