在开发项目中有些常用的的实用代码(ps:平时看着无关紧要的,却很容易忘记)
1,在客户端使用Cookie
document.cookie = "key=1";
document.cookie = "name=zhangsan";
cookie中存值形式:key=1,name=zhangsan等
// 刷新、关闭 页面之前,清理cookie中的key值。
window.onbeforeunload = function () {
// alert(document.cookie);
};
//从cookie中获取数据
function getCookie(cookie_name) {
var result = "";
var allcookies = document.cookie;
var cookie_pos = allcookies.indexOf(cookie_name);
if (cookie_pos != -1) {
cookie_pos += cookie_name.length + 1;
var cookie_end = allcookies.indexOf(";", cookie_pos);
if (cookie_end == -1) {
cookie_end = allcookies.length;
}
result = unescape(allcookies.substring(cookie_pos, cookie_end));
}
return result;
}
2.在js中读取//读txt文件 使用到的微软插件时:activeObject;
function readFile(filename) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename, 1);
var s = "";
while (!f.AtEndOfStream)
s += f.ReadLine() + "\n";
f.Close();
return s;
}
后期不断更新中...