欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP中js传递和解析URL参数以及中文转码和解码问题

程序员文章站 2023-01-03 22:52:37
1.传递参数: 复制代码 代码如下: var pmt = 'sensor='+ encodeuri(encodeuri(sensor))+'&device='+encode...
1.传递参数:
复制代码 代码如下:

var pmt = 'sensor='+ encodeuri(encodeuri(sensor))+'&device='+encodeuri(encodeuri(device))+'&instrument=';
pmt += encodeuri(encodeuri(instrument))+'&n='+n+'&adddate='+adddate;
top.location.href = 'jsp/print/diagnosticanaprint.jsp?'+pmt;

2.接收和解析参数
复制代码 代码如下:

//获取url参数
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 sensor;//测点
sensor = decodeuri(decodeuri(request['sensor']));
3.参考内容:
方法一:正则分析法
复制代码 代码如下:

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"));

方法二:
复制代码 代码如下:

<span style="font-size: 16px;"><script language="javascript">
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;
}
</script></span>
这样调用: 
<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>