六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 32|回复: 0

js获取页面url的方法

[复制链接]

升级  3.33%

11

主题

11

主题

11

主题

秀才

Rank: 2

积分
55
 楼主| 发表于 2013-2-4 22:58:54 | 显示全部楼层 |阅读模式
1, window.location.href
整个URl字符串(在浏览器中就是完整的地址栏)
本例返回值: http://ifisker.com/blog/post/0703/window.location.html#

2,window.location.protocol
URL 的协议部分
本例返回值:http:

3,window.location.host
URL 的主机部分
本例返回值:www.x2y2.com

4,window.location.port
URL 的端口部分
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符
本例返回值:""

5,window.location.pathname
URL 的路径部分(就是文件地址)
本例返回值:/fisker/post/0703/window.location.html

6,window.location.search
查询(参数)部分
除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值
本例返回值:?ver=1.0&id=6

7,window.location.hash
锚点
本例返回值:#imhere




URL的参数&参数名1=XXXX&参数名2=XXXX&参数名3=XXXX   

function    GetQueryString(name)   
   {   
       var    reg   =   new    RegExp("(^|&)"+    name   +"=([^&]*)(&|$)");   
       var    r   =    window.location.search.substr(1).match(reg);   
      if    (r!=null)   return    unescape(r[2]);   return   null;   
   }   
   alert(GetQueryString("参数名1"));   
   alert(GetQueryString("参数名2"));   
   alert(GetQueryString("参数名3"));
如:http://www.cnblogs.com/EditPosts.aspx?a=1&b=2&c=3

b = GetQueryString("b"); [b=2]

转换成数字parseInt(num)




function   getUrlParam(name){   
          var   reg   =   new   RegExp("(^|&)"+   name   +"=([^&]*)(&|$)");   
          var   r   =   window.location.search.substr(1).match(reg);   
          if   (r!=null)   return   unescape(r[2]);   return   null;
      }
如果aa.aspx?id=3
调用时只需getUrlParam("id")即可以得到3
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表