当设置锚的href属性时,相对URL变为绝对URL

Relative URL Changes to Absolute When Setting the href Attribute of an Anchor

本文关键字:URL 相对 设置 href 属性      更新时间:2023-09-26
var a = document.createElement('a');
a.href = '/path';
// will give the absolute url: http://.../path
console.log(a.href);

是否有可能a.href将返回原始的相对路径'/path'而不修改返回值之后?

应该可以了-

var a = document.createElement('a');
a.href = '/path';
var domain = window.location.origin;
console.log(a.href.substring(domain.length));