URL.canParse API All In One
URL.canParse API All In One
// Proper usage
if (URL.canParse('https://davidwalsh.name/pornhub-interview')) {
const parsed = new URL('https://davidwalsh.name/pornhub-interview');
}
https://caniuse.com/?search=canParse
URL Standard
Node.js
https://nodejs.org/api/url.html#urlcanparseinput-base
MDN
The URL.canParse()
static method of the URL interface returns a boolean
indicating whether or not an absolute URL, or a relative URL combined with a base URL, are parsable
and valid
.
URL 接口的 URL.canParse()
静态方法返回一个布尔值
,指示绝对 URL 或与基本 URL 组合的相对 URL 是否可解析
且有效
。
URL.canParse(url)
URL.canParse(url, base)
https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static
demos
URL.canParse() not supported
❌
const logElement = document.getElementById("log");
function log(text) {
logElement.innerText += `${text}\n`;
}
if ("canParse" in URL) {
log("Test valid absolute URL");
let url = "https://developer.mozilla.org/";
let result = URL.canParse(url);
log(` URL.canParse("${url}"): ${result}`);
log("\nTest relative URL with no base URL");
url = "/en-US/docs";
result = URL.canParse(url);
log(` URL.canParse("${url}"): ${result}`);
log("\nTest relative URL with valid base URL");
let baseUrl = "https://developer.mozilla.org/";
result = URL.canParse(url, baseUrl);
log(` URL.canParse("${url}","${baseUrl}"): ${result}`);
} else {
log("URL.canParse() not supported");
}
if ("canParse" in URL) {
log("\nTest relative URL with base URL supplied as a URL object");
let baseUrl = new URL("https://developer.mozilla.org/");
let url = "/en-US/docs";
result = URL.canParse(url, baseUrl);
log(` URL.canParse("${url}","${baseUrl}"): ${result}`);
}
https://developer.mozilla.org/en-US/play
https://twitter.com/stefanjudis/status/1676519268819542016
refs
https://davidwalsh.name/url-canparse
https://github.com/whatwg/url/issues/713
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17581341.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2022-07-25 Linux file system All In One
2022-07-25 开元路土魠鱼羹 bug All In One
2021-07-25 webpack Public Path All In One
2020-07-25 Chrome blocked third-party cookies
2020-07-25 JavaScript 中如何使用 setTimeout 模拟实现 setInterval All In One
2019-07-25 Service Workers
2019-07-25 IndexedDB All In One