设置锚点的href属性时,相对URL更改为绝对值

时间:2015-10-27 12:05:07

标签: javascript jquery url dom

var a = document.createElement('a');
a.href = '/path';

// will give the absolute url: http://.../path
console.log(a.href);

a.href是否有可能在不修改返回值的情况下返回原始相对路径'/ path'?

1 个答案:

答案 0 :(得分:0)

这应该这样做 -

var a = document.createElement('a');
a.href = '/path';

var domain = window.location.origin;
console.log(a.href.substring(domain.length));