URL地址参数详解

URL (Uniform Resource Locator):统一资源定位符。

完整的URL构成:scheme://host:port/path?query#fragment(传输协议+域名:端口+资源路径)

  1. scheme:通信协议,常用的有http,ftp等
  2. host:请求的服务器地址+port(端口,默认80)
  3. query:查询可选,可传递多个参数,用“&”隔开,用=赋值
  4. fragment:信息片段字符串,用于制定网络资源中的片段。比如一个网页有多个名次解释,可使用fragment直接定位到某一名次解释

对应的Javeascript语句

  1. window.location.href  获取整个URL字符串
  2. window.location.protocolURL  返回协议部分(http:)
  3. window.location.hostURL   返回服务器地址
  4. window.location.portURL    返回端口
  5. window.location.pathnameURL  返回文件路径
  6. window.location.search  查询(参数)部分除了给动态语言赋值以外,我们同样可以给静态页面
  7. window.location.hash  返回锚点

使用正则表达式来获取地址的参数

 
  1. function GetQueryString(name)

  2. {

  3. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");

  4. var r = window.location.search.substr(1).match(reg);

  5. if(r!=null)return unescape(r[2]); return null;

    扫描二维码关注公众号,回复: 5992146 查看本文章
  6. }

  7.  
  8. // 调用方法

  9. alert(GetQueryString("参数名1"));

  10. alert(GetQueryString("参数名2"));

  11. alert(GetQueryString("参数名3"));

猜你喜欢

转载自blog.csdn.net/houyichaochao/article/details/81456644