Home > Web Front-end > JS Tutorial > js method to get the value passed by url_javascript skills

js method to get the value passed by url_javascript skills

WBOY
Release: 2016-05-16 15:24:46
Original
2853 people have browsed it

本文实例讲述了js获取url传值的方法。分享给大家供大家参考,具体如下:

js获取url参数值:

index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&......

静态html文件js读取url参数 根据获取html的参数值控制html页面输出

一、字符串分割分析法

这里是一个获取URL带QUESTRING参数的JAVASCRIPT客户端解决方案,相当于asp的request.querystring,PHP的$_GET

函数:

<Script language="javascript">
function GetRequest() {
var url = location.search; //获取url中"&#63;"符后的字串
var theRequest = new Object();
if (url.indexOf("&#63;") != -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;
}
</Script>

Copy after login

然后我们通过调用此函数获取对应参数值:

<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var 参数1,参数2,参数3,参数N;
参数1 = Request['参数1'];
参数2 = Request['参数2'];
参数3 = Request['参数3'];
参数N = Request['参数N'];
</Script>

Copy after login

以此获取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"));

Copy after login

补充:js 获取url的get传值函数

function getvl(name) {
  var reg = new RegExp("(^|\\&#63;|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
  if (reg.test(location.href))
  return unescape(RegExp.$2.replace(/\+/g, " "));
  return "";
};

Copy after login

用法简单的说一下

比如url是:http://localhost/index.php?qz=ddddk中qz的值代码如下

复制代码 代码如下:
getvl(bb);//这样就可以了

希望本文所述对大家JavaScript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template