javascript获取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("userName"))
第二种 直接根据字符串截取参数
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
扫描二维码关注公众号,回复: 1003324 查看本文章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 参数1,参数2,参数3,参数N;
参数1 = Request['参数1'];
参数2 = Request['参数2'];
参数3 = Request['参数3'];
参数N = Request['参数N'];
javascript打开新窗口、页面
1、 //在同当前窗口中打开窗口
超链接
<a href="http://www.baidu.com" title="这是一个新页面">Welcome</a>
等效于js代码
window.location.href="http://www.baidu.com";
2、 //在另外新建窗口中打开窗口
超链接
<a href="http://www.baidu.com" title="这是一个新页面" target="_blank">Welcome</a>
等效于js代码
window.open("http://www.baidu.com");
js控制打开的新窗口
function openwin() {
window.open ("链接地址", "newwindow", "height=100, width=400, toolbar =no, menubar=no,scrollbars=no, resizable=no, location=no, status=no") //写成一行
}
关闭窗口 window.close()
js获取参数为乱码的解决办法
传参数时使用encodeURI(url)
function send(){
var url = "NewFile.html";
var userName = $("#userName").html();
window.open(encodeURI(url + "?userName=" + userName));
}
接收参数时使用decodeURI(参数名)
function get(){
var urlinfo = window.location.href;
var userName = urlinfo.split("?")[1].split("=")[1];
alert(decodeURI(userName));
}