我有这样的链接:
<a href="./somepage.html">This link goes to some page.</a>
在访问该页面后,我有一个包含页面的pathname
的变量。它看起来像这样:
/C:/work/so/test/somepage.html
我正在通过这样做来比较它们:
curLocation = window.location.pathname;
pathname = curLocation.substring(0, curLocation.lastIndexOf('/') + 1) + elements[i].getAttribute('href');
var oldPathname = '/C:/work/so/test/somepage.html'; //pretend this variable is already set, and not hardcoded
if(pathname == oldPathname) {
//do stuff
}
但是这不起作用,因为pathname
将等于:
/C:/work/so/test/./somepage.html
...但是oldPathname
(导航该链接后的实际结果路径)将如下所示:
/C:/work/so/test/somepage.html
如何可靠地检查这两个链接是否相同?有没有办法“模拟”点击链接,找到结果window.location.pathname
会是什么?
答案 0 :(得分:1)
只需使用.href而不是getAttribute(&#39; href&#39;)
document.querySelectorAll('a').forEach(a => console.log(a.href))
&#13;
<a href="./link1.html">Link 1</a>
<a href="../link2.html">Link 2</a>
<a href="../folder/link3.html">Link 3</a>
&#13;