JavaScript获取URL各部分详解
在WEB开发过程中,经常需要利用JavaScript来提取当前页面的URL相关信息。以下是对获取URL各项信息的常见方法进行总结。
为了便于说明,下面以具体的URL为例,展示如何拆解其各个组成部分:http://i.jb51.net/EditPosts.aspx?opt=1
1、window.location.href(用于设置或获取完整的URL字符串)
var test = window.location.href; alert(test); 返回结果:http://i.jb51.net/EditPosts.aspx?opt=1
2、window.location.protocol(用于设置或获取URL的协议部分)
var test = window.location.protocol; alert(test); 返回结果:http:
3、window.location.host(用于设置或获取URL的主机部分)
var test = window.location.host; alert(test); 返回结果:i.jb51.net
4、window.location.port(用于设置或获取URL关联的端口号)
var test = window.location.port; alert(test); 返回结果:空字符(注:若使用默认的80端口,即使显式添加:80,返回值仍为空字符而非80)
5、window.location.pathname(用于设置或获取URL的路径部分,即文件地址) var test = window.location.pathname; alert(test); 返回结果:/EditPosts.aspx
6、window.location.search(用于设置或获取href属性中问号后面的部分)
var test = window.location.search; alert(test); 返回结果:?opt=1
PS:获取查询参数部分不仅可用于动态语言赋值,也可用于静态页面,通过JavaScript读取相应的参数值。
7、window.location.hash(用于设置或获取href属性中井号“#”后面的分段)
var test = window.location.hash; alert(test); 返回结果:空字符(因示例URL中无此部分)
8、JS获取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;
}
// 这样调用:
alert(GetQueryString("参数名1"));
alert(GetQueryString("参数名2"));
alert(GetQueryString("参数名3"));
二、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;
}
var Request = new Object();
Request = GetRequest();
// var id=Request["id"];
// var 参数1,参数2,参数3,参数N;
// 参数1 = Request['参数1'];
// 参数2 = Request['参数2'];
// 参数3 = Request['参数3'];
// 参数N = Request['参数N'];
三、指定取
例如一个URL:http://i.jb51.net/?j=js,若要获取参数j的值,可通过以下函数调用实现。
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
var context = "";
if (r != null)
context = r[2];
reg = null;
r = null;
return context == null || context == "" || context == "undefined" ? "" : context;
}
alert(GetQueryString("j"));
四、单个参数的获取方法
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
if (url.indexOf("?") != -1) { //判断是否有参数
var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
strs = str.split("="); //用等号进行分隔 (因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔)
alert(strs[1]); //直接弹出第一个参数 (如果有多个参数 还要进行循环的)
}
}
JavaScript获取当前URL参数的两种主要方法
//返回的是字符串形式的参数,例如:class_id=3&id=2&
function getUrlArgStr(){
var q=location.search.substr(1);
var qs=q.split('&');
var argStr='';
if(qs){
for(var i=0;i
此外,列出一些JavaScript获取URL各部分的功能方法供参考:
window.location.host; //返回URL的主机部分,例如:www.xxx.com window.location.hostname; //返回www.xxx.com window.location.href; //返回整个URL字符串(浏览器中即完整地址栏),例如:www.xxx.com/index.php?class_id=3&id=2 window.location.pathname; //返回/a/index.php或者/index.php window.location.protocol; //返回URL的协议部分,例如: http:,ftp:,maito:等。 window.location.port //URL的端口部分,若采用默认80端口,返回值并非80而是空字符
以上内容涵盖了获取当前页面URL网址信息的主要方法,希望对理解和使用有所帮助。