在HTML中获取正确的URL属性值
在HTML中,常见的URL有多种表示方式:
相对URL:
example.php
demo/example.php
./example.php
../../example.php
/example.php
绝对URL:
http://dancewithnet.com/example.php
http://dancewithnet.com:80/example.php
https://dancewithnet.com/example.php
同时HTML中有大量的元素属性值为URL,一般利用JavaScript获取这些URL属性值有两种方法:
<a href="example.php" id="example-a">此时页面绝对URL是http://dancewithnet.com/</a>
<script>
var oA = document.getElementById('example-a');
oA.href == 'http://dancewithnet.com/example.php';
oA.getAttribute('href') == 'example.php';
</script>
我们希望通过直接访问属性的方式得到完整绝对URL,通过getAttribute方法得到其原始的属性值,实际上这是一个比较理想的结果,在所有的A级浏览器中,能顺利得到这个结果的只有Firefox和IE8,其他浏览器都或多或少特殊情况,具体哪些元素的属性存在什么样的情况请看演示实例。
在大部分浏览器中存在的问题是,两种方式都返回的是原始属性值,而实际应用中往往需要的是其绝对的URL,《Dealing with unqualified HREF values》中的解决方案太过于复杂,这里提供一种相对简单的解决方案,如果不考虑区别浏览器代码会非常简单:
<form action="example.php" id="example-form">
此时页面绝对URL是http://dancewithnet.com/</form>
<script>
var oForm = document.getElementById('example-form');
//IE6、IE7、Safari、Chrome、Opera
oForm.action == 'example.php';
oForm.getAttribute('action') == 'example.php';
//获取绝对URL的通用解决方案
getQualifyURL(oForm,'action') == 'http://dancewithnet.com/example.php';
getQualifyURL = function(oEl,sAttr){
var sUrl = oEl[sAttr],
oD,
bDo = false;
//是否是IE8之前版本
//http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/
//http://msdn.microsoft.com/en-us/library/7kx09ct1%28VS.80%29.aspx
/*@cc_on
try{
bDo = @_jscript_version < 5.8 ?true : @false;
}catch(e){
bDo = false;
}
@*/
//如果是Safari、Chrome和Opera
if(/a/.__proto__=='//' || /source/.test((/a/.toString+''))
|| /^function \(/.test([].sort)){
bDo = true;
}
if(bDo){
oD = document.createElement('div');
/*
//DOM 操作得到的结果不会改变
var oA = document.createElement('a');
oA.href = oEl[sAttr];
oD.appendChild(oA);
*/
oD.innerHTML = ['<a href="',sUrl,'"></a>'].join('');
sUrl = oD.firstChild.href;
}
return sUrl;
}
</script>
在IE6和IE7这两个史前的浏览器身上还有一些更有意思的事情,两种方法在HTML元素A、AREA和IMG获取的属性值都是绝对URL,幸好微软为getAttribute提供了第二个参数可以解决这个问题,同时还可以对IFEAM和LINK元素解决前面提到的两种方法都返回原始属性的问题:
<link href="../../example.css" id="example-link">
<a href="example.php" id="example-a">此时页面绝对URL是http://dancewithnet.com/</a>
<script>
var oA = document.getElementById('example-a'),
oLink = document.getElementById('example-link');
oA.href == 'http://dancewithnet.com/example.php';
oA.getAttribute('href') == 'http://dancewithnet.com/example.php';
oA.getAttribute('href',2) == 'example.php';
oLink.href == 'example.php';
oLink.getAttribute('href') == 'example.php';
oLink.getAttribute('href',4) == 'http://dancewithnet.com/example.php';
</script>
今天突然看到getAttribute(‘href’,2)还能解决获取IE6下中文URL的乱码问题,虽然以前没有遇到过,但作为针对IE6怪癖的记录还是值得了解的。
标签:getAttribute, HTML, URL
作者:秦歌,时间:2009-07-27 23:57,归纳于:Javascript & DOM & AJAX,订阅:RSS 2.0,引用:Trackback



我是来抢占撒花的
如果获取href那么难,那么使用自定义属性吧。
这个麻烦没怎么碰到过。
好麻烦的
还真没有考虑过弄个url会那么麻烦~
各家浏览器的BOM不统一,以及对W3C标准的理解不一,导致了很多前端开发的困扰啊。
[...] « 在HTML中获取正确的URL属性值 [...]
http://dancewithnet.com/lab/2009/get-right-url-from-html/index.html
演示示例没写哪些结果对应哪些浏览器呀
我晕,文章中不是说的很清楚么,演示示例中在每个浏览器中都有结果,只是有些符合预期,有些不符合,这个和具体的标签有关,把每个浏览器的每种情况都列出来意义不大,如果需要自己用浏览器看一遍就知道了,本文告诉的是通用的解决方案。
秦秦兄,小弟眼瞎…
[...] 原文:http://dancewithnet.com/2009/07/27/get-right-url-from-html/ [...]
问楼主个问题,我的项目中是使用的ie8,可是页面头加入了
这一句,所以
<a href=”example.php” rel=”nofollow”>http://dancewithnet.com/</a>
var oA = document.getElementById(‘example-a’);
alert(oA.getAttribute(‘href’));
用你的代码试了下,只是得到a 的相对url ,如果DOCTYPE 不用xhtml就没有问题,难道只能放弃xhtml1.0 ??
@mefly:我想你应该没有认真看这篇blog,这里面的内容就是告诉你如何得到想要的结果。IE8在标准模式下getAttribute得到的是属性值,在怪癖模式下IE8的解析模式和IE5一致而得到是绝对URL,所以和是否使用XHTML无关。你完全可以通过Element.href来兼容IE下两个模式,或者使用本文getQualifyURL函数。