是否始终将a.href属性返回为绝对值?
在以下HTML中:
<a id="link" href="page.htm">Page</a>
我发现 document.getElementById("link").href
始终将绝对路径返回到 page.htm
,而不是我键入的相对路径它.参见 http://jsfiddle.net/4HgAW/.
I'm finding that document.getElementById("link").href
always returns the absolute path to page.htm
rather than the relative path as I have typed it. See http://jsfiddle.net/4HgAW/.
我可以保证在所有浏览器中javascript都将始终返回绝对路径吗?
我要问的原因是我要抓住 http://www ....
部分来检查链接指向的域,并且我需要它在内部工作链接.
The reason I'm asking is that I'm grabbing the http://www....
part to check which domain the link points to, and I need it to work for internal links as well.
是的,所有相关的浏览器都返回完全限定的URL.
Yes, all relevant browsers return the fully qualified URL.
如果要检索 href
属性的原始值(在本例中为'page.html'),可以执行以下操作:
If you want to retrieve the original value of the href
attribute ('page.html' in this case), you can do this:
anchor.getAttribute('href')
但是,在旧版本的IE(8及更低版本)中,这似乎不起作用.
However, that doesn't seem to work in older versions of IE (8 and below).