base64记录查询参数!
https://github.com/dankogai/js-base64
Base64.encode('dankogai'); // ZGFua29nYWk=
Base64.encode('小飼弾'); // 5bCP6aO85by+
Base64.encodeURI('小飼弾'); // 5bCP6aO85by-
Base64.decode('ZGFua29nYWk='); // dankogai
Base64.decode('5bCP6aO85by+'); // 小飼弾
// note .decodeURI() is unnecessary since it accepts both flavors
Base64.decode('5bCP6aO85by-'); // 小飼弾
首页,获取查询参数!
/**
* [获取URL中的参数名及参数值的集合]
* 示例URL:http://htmlJsTest/getrequest.html?uid=admin&rid=1&fid=2&name=小明
* @param {[string]} urlStr [当该参数不为空的时候,则解析该url中的参数集合]
* @return {[string]} [参数集合]
*/
function GetRequest(urlStr) {
if (typeof urlStr == "undefined") {
var url = decodeURI(location.search); //获取url中"?"符后的字符串
} else {
var url = "?" + urlStr.split("?")[1];
}
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]] = decodeURI(strs[i].split("=")[1]);
}
}
return theRequest;
}
var request = Base64.encode(JSON.stringify(GetRequest()));
console.log(Base64.decode(request));
if (request) {
var tail = '&request=' + request;
} else {
var tail = '';
}
传入编辑页面
$(".edit").on('click', function () {
var id = $(this).data('id');
window.location.href = '__APP__/Product/edit?id=' + id + tail;
});
后台获取,并传入
$request = $_GET['request'];
$this->assign('request', $request);
<input type="hidden" name="request" id="request" value="{$request}" />
取消,或者完成编辑的时候,解析Base64
var request = $("#request").val();
if (request) {
var str = '&request=' + request;
} else {
var str = '';
}
$("#cancel_btn").on('click', function () {
// 解密参数
var request = JSON.parse(Base64.decode($("#request").val()));
let tail = '';
for(let key in request){
if (tail == '') {
tail += '?' + key + '=' + request[key];
} else {
tail += '&' + key + '=' + request[key];
}
console.log(key + '---' + request[key])
}
window.location.href = 'index'+tail;
});
$("#add_form").ajaxForm({
dataType: "json",
success: function (obj) {
if (obj.errno == 0) {
alert('添加编辑完成');
var request = JSON.parse(Base64.decode($("#request").val()));
let tail = '';
for (let key in request) {
if (tail == '') {
tail += '?' + key + '=' + request[key];
} else {
tail += '&' + key + '=' + request[key];
}
console.log(key + '---' + request[key])
}
window.location.href = 'index' + tail;
} else {
alert(obj.errdesc);
}
return false;
}
});
小结
base64的好处是能够存储无数个参数!一个参数,能够解决传入参数问题!