js获取地址栏中链接的参数
我的地址栏是这样的
C:/Users/Administrator/Desktop/test/demo1.html?id=1&type=2
/*
* 获取链接参数
*/
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
var id = getUrlParam('id'); // id=1
var type = getUrlParam('type'); // type=2
console.log('id===>', id);
console.log('type ===>', type);
效果图
js设置cookie和获取cookie
//写Cookie
function addCookie(objName, objValue, objHours) {
var str = objName + "=" + escape(objValue); //编码
if (objHours > 0) { //为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours * 3600 * 1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
}
//读Cookie
function getCookie(objName) { //获取指定名称的cookie的值
var arrStr = document.cookie.split("; ");
for (var i = 0; i < arrStr.length; i++) {
var temp = arrStr[i].split("=");
if (temp[0] == objName) return unescape(temp[1]); //解码
}
return "";
}
//移除Cookie
function delCookie(objName) {
var exp = new Date();
exp.setTime(exp.getTime() - 60 * 60 * 1000);
var cval = getCookie(objName);
if (cval != null)
document.cookie = objName + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
}