本文共 1490 字,大约阅读时间需要 4 分钟。
URL : 统一资源定位符 (Uniform Resource Locator, URL) 完整的URL由这几个部分构成: scheme://host:port/path?query#fragment scheme = 通信协议 (常用的http,ftp,maito等) host = 主机 (域名或IP) port = 端口号 path = 路径 query = 查询可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技术制作的网页)传递参数,可有多个参数,用”&”符号隔开,每个参数的名和值用”=”符号隔开。 fragment = 信息片断字符串,用于指定网络资源中的片断。例如一个网页中有多个名词解释,可使用fragment直接定位到某一名词解释。(也称为锚点.)对于这样一个URL我们可以用javascript获得其中的各个部分 1, window.location.href整个URl字符串(在浏览器中就是完整的地址栏) 2,window.location.protocol URL 的协议部分本例返回值:http: 3,window.location.host URL 的主机部分本例返回值: 4,window.location.port URL 的端口部分如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符本例返回值:”" 5,window.location.pathname URL 的路径部分(就是文件地址)本例返回值:/seo/ 6,window.location.search查询(参数)部分除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值本例返回值:?ver=1.0&id=6 7,window.location.hash锚点本例返回值:#imhere
8. url参数值
方法一:正则分析法
function getQueryString(name) {var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");var r = window.location.search.substr(1).match(reg);if (r != null) return unescape(r[2]); return null;}
方法二:采用split拆成数组
function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest;}
本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/archive/2013/01/11/2857099.html ,如需转载请自行联系原作者