您的位置:首页 > Web前端 > JavaScript

IE下js读取href值的相对路径和绝对路径的问题

2009-11-19 16:07 369 查看
通过JavaScript读取链接href的属性值是,IE有时候会读取出来的值是绝对路径,而其他标准浏览器如火狐则是原始路径。

在IE中,对于JavaScript的getAttribute方法,IE还提供了第二个参数,值分别是0,1,2。为2时取原始路径值,为1是取服务器绝对路径。

看一下代码就清楚了:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<div id="test">    <a href="../index.php" mce_href="index.php"> test </a></div><div id="result"></div>
<mce:script type="text/javascript"><!--
var test = document.getElementById('test');
var link = test.getElementsByTagName('a');
linkhref = link[0].getAttribute('href');
alert(linkhref);
linkhref = link[0].getAttribute('href',0);
alert(linkhref);
linkhref = link[0].getAttribute('href',1);
alert(linkhref);
linkhref = link[0].getAttribute('href',2);
alert(linkhref);

// --></mce:script>
</body>
</html>


参考文章:http://www.planabc.net/2008/11/06/ie-href-bug/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: