在HTML中,常见的URL有多种表示方式:

来源:随网之舞 时间:2009-07-28 12:18:00 


在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、OperaoForm.action ==  'example.php';oA.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-a');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>

标签:url,javascript,html,绝对URL
0
投稿

猜你喜欢

  • 如何使用ASP实现网站的“目录树”管理

    2008-06-13 06:39:00
  • encodeURIComponent用法UrlEncode与URLEncode.encode()

    2009-05-11 12:40:00
  • ASP教程:制作登陆验证页面程序

    2008-10-23 15:00:00
  • DHTML+XML+ASP+CSS的树形目录

    2008-10-24 14:29:00
  • 巧用overflow属性解决中间间距问题

    2007-12-08 20:26:00
  • 使用网际数据库浏览器在线维护Access数据库

    2008-05-23 13:05:00
  • WEB3.0时代的开放与聚合

    2008-08-21 17:19:00
  • 懒懒交流会:ClassName的长命名 VS. 短命名

    2009-11-28 16:08:00
  • ASP 根据用户权限判断显示的列标题

    2011-03-29 11:01:00
  • gem install mysql报错checking for mysql_qu

    2010-11-11 12:13:00
  • Javascript世界的最大整数值

    2008-06-23 13:23:00
  • 探讨链接打开方式

    2009-03-16 16:55:00
  • SQLServer 镜像功能完全实现

    2011-09-30 11:33:07
  • sql server update 表的问题

    2009-10-04 20:32:00
  • 教你快速掌握一些方便易用的SQL语句

    2008-11-28 15:21:00
  • 利用SQL语句对不同数据库进行高效果分页

    2008-11-28 14:44:00
  • 简单form标准化实例——整体布局

    2007-05-11 17:04:00
  • 关于网站地图

    2011-01-06 12:14:00
  • Web设计色彩速查表

    2009-12-21 16:24:00
  • XML:OpenSearch 浏览器指定搜索应用

    2010-05-04 19:37:00
  • asp之家 网络编程 m.aspxhome.com