DOMParser 中的可选 baseURI(位置)?
Optional baseURI (location) in DOMParser?
// html data loaded from [http://example.org/some/path]
let htmlSource = `<!DOCTYPE html><html><head></head><body>
... <a href="relative"></a> ...
</body></html>`;
const dom = new DOMParser().parseFromString(htmlSource, 'text/html');
// this returns the value of the attribute as is
dom.links[0].getAttribute('href'); // -> "relative" / nothing new here
// this should resolve urls
dom.links[0].href // -> wait, relative to WHAT??
// if you run this code while being on [http://google.com] you'll get
dom.links[0].href // -> "https://www.google.com/relative"
// Also,
dom.location // -> null / you can't change it
dom.baseURI // -> "https://www.google.com/" / read-only
所以看起来 DOMParser
隐式强制使用当前页面 location
作为新 HTMLDocument
的 baseURI
。
为什么不给开发人员一个选项(第三个参数?)来明确指定文档位置?
有没有办法让 DOMParser
尊重可选基数 url?解决方法?
您必须在 html 代码中使用 base
标记,您甚至可以动态添加它。这是您的示例:
const htmlSource = `
<!DOCTYPE html>
<html>
<head>
<base href="https://www.example.com/">
</head>
<body>
<a href="relative"></a>
</body>
</html>`;
const dom = new DOMParser().parseFromString(htmlSource, 'text/html');
console.log('DOM link ->', dom.links[0].href);
将输出:
DOM link -> https://www.example.com/relative
动态添加:
let baseEl = dom.createElement('base');
baseEl.setAttribute('href', 'https://www.example.com');
dom.head.append(baseEl);
// html data loaded from [http://example.org/some/path]
let htmlSource = `<!DOCTYPE html><html><head></head><body>
... <a href="relative"></a> ...
</body></html>`;
const dom = new DOMParser().parseFromString(htmlSource, 'text/html');
// this returns the value of the attribute as is
dom.links[0].getAttribute('href'); // -> "relative" / nothing new here
// this should resolve urls
dom.links[0].href // -> wait, relative to WHAT??
// if you run this code while being on [http://google.com] you'll get
dom.links[0].href // -> "https://www.google.com/relative"
// Also,
dom.location // -> null / you can't change it
dom.baseURI // -> "https://www.google.com/" / read-only
所以看起来 DOMParser
隐式强制使用当前页面 location
作为新 HTMLDocument
的 baseURI
。
为什么不给开发人员一个选项(第三个参数?)来明确指定文档位置?
有没有办法让 DOMParser
尊重可选基数 url?解决方法?
您必须在 html 代码中使用 base
标记,您甚至可以动态添加它。这是您的示例:
const htmlSource = `
<!DOCTYPE html>
<html>
<head>
<base href="https://www.example.com/">
</head>
<body>
<a href="relative"></a>
</body>
</html>`;
const dom = new DOMParser().parseFromString(htmlSource, 'text/html');
console.log('DOM link ->', dom.links[0].href);
将输出:
DOM link -> https://www.example.com/relative
动态添加:
let baseEl = dom.createElement('base');
baseEl.setAttribute('href', 'https://www.example.com');
dom.head.append(baseEl);