Skip to content

Instantly share code, notes, and snippets.

@shenqihui
Created July 6, 2014 10:22
Show Gist options
  • Save shenqihui/2477d48579190da5adbd to your computer and use it in GitHub Desktop.
Save shenqihui/2477d48579190da5adbd to your computer and use it in GitHub Desktop.
"use strict";
var util = util || {};
// cookie 相关函数
util.setCookie = function(name, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}
util.setCookieSecond = function(name, value, second) {
var exdate = new Date()
exdate.setSeconds(exdate.getSeconds() + second)
document.cookie = name + "=" + escape(value) +
((second == null) ? "" : ";expires=" + exdate.toGMTString())
}
util.getCookie = function(name) {
var start, end;
if (document.cookie.length > 0) {
start = document.cookie.indexOf(name + "=")
if (start != -1) {
start = start + name.length + 1
end = document.cookie.indexOf(";", start)
if (end == -1) end = document.cookie.length
return unescape(document.cookie.substring(start, end))
}
}
return ""
}
// function checkCookie() {
// var username = getCookie('username');
// }
// cookie 相关函数
// 监控网页错误情况
//接受三个参数,分别是错误信息,错误页面的url和错误行号
window.onerror = function(message, url, linenumber) {
var devMod = window.devMod || false;
if (devMod === true) {
// 表示此前是开发环境
return;
}
console.log("window.onerror", message, url, linenumber);
if (typeof message === "string") {
if ((message.search('is not a function') > 0) || (message.search('is not defined') > 0) || (message.search('of undefined') > 0)) {
// 方法名出错的问题。// 没定义变量就使用的情况 //直接使用没定义变量的情况
util.reflashPageByError();
}
}
}
util.reflashPageByError = function(url) {
url = url || location.href;
var time,
cookiesVal;
time = parseInt(util.getCookie(url)) || 0;
time++;
if (time < 4) {
// 120 秒以内只能刷新 3 次,120秒之后自动cookies消失,重新能够使用 location.href = location.href;
util.setCookieSecond(url, time, 120);
location.href = location.href;
}
}
// | 监控网页错误情况
// url 参数获取
util.getUrlParam = function(name) {
var href = transSearch || location.href,
start,
end;
if ( href.length > 0 ) {
start = href.indexOf(name + "=");
if (start != -1) {
start = p_start + name.length + 1;
end = href.indexOf("&", start);
if (end == -1) end = href.length;
return decodeURIComponent(unescape(href.substring(start, end)));
}
}
return ""
}
// |url 参数获取
/**
* @name buildUrlEscapeParam
* @desc url 参数化
* @depend []
* @param { string } str 网址 href
**/
util.buildUrlEscapeParam = function(str) {
str = str || "";
var newStr = str;
var oldStr = str;
var subStr;
oldStr = oldStr.replace('&', 'ppppppppppppppppppppppp');
var paramStart = oldStr.indexOf('?');
if ( oldStr.length > 0 ){
if( paramStart != -1 ){
paramStart += 1;
newStr = oldStr.substring(0, paramStart);
subStr = oldStr.substring(paramStart);
newStr += escape(encodeURIComponent(subStr));
newStr = newStr.replace('ppppppppppppppppppppppp', '&');
}
}
// 进行优化 query
newStr.replace('?&', '&').replace(/&+/,'&');
return newStr;
}
/**
* @name getUrlEscapeParam
* @desc url 非参数化
* @depend []
* @param { string } str 网址 href
**/
util.getUrlEscapeParam = function(str) {
return decodeURIComponent( unescape( decodeURIComponent( unescape( str ) ) ) );
}
// 先进行下获取
window.transSearch = util.getUrlEscapeParam( location.search.substring(1) );
( window.devMod === true ) && console.log( transSearch );
// util不进行冲突处理
window.util = window.util || util;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment