阻止冒泡、默认行为、事件捕获
/* funname preventEventPropagation
* desc 阻止冒泡事件&阻止默认行为&阻止事件捕获
* params {name: string}参数名称
* return {string or null}
*/
var preventEventPropagation = function(evt) {
var e = evt || window.event;
if (e) {
e.preventDefault();
e.stopPropagation();
}
return false;
};
获取地址参数
/* funname getQueryString
* desc 获取地址参数
* params {name: string}参数名称
* return {string or null}
*/
var getQueryString = function(name) {
var r, reg;
reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
r = window.location.search.substr(1).match(reg);
if (r !== null) {
return r[2]; //不解码
}
return null;
};
判断微信环境
/*
*desc:判断微信环境
*params:null
*/
var isInWechat = function() {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
return true;
} else {
return false;
}
};
获取环境参数
/*
*funname:platformFn
*des:获取环境参数
*params:
*return: [object]
*/
var platformFn = function() {
var u = navigator.userAgent,
app = navigator.appVersion;
return {
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
};
};
判断是否为数组
// 判断是否为数组
/*
*funname:isArray
*des:判断是否为数组
*params: [array] arr
*return: [bolean]
*/
function isArray(arr) {
if (arr typeof arr === 'object' && arr.constructor == Array) {
return true;
}
return false;
}
数组除重
// 数组除重
/*
*funname:arrayUnique
*des:数组去重
*params: [array] arr
*return: [array]
*/
var arrayUnique = function(arr) {
var newArr = [];
var temp = {};
var len = arr.length;
for (var index = 0; index < len; index++) {
var key = typeof(arr[index]) + arr[index];
if (temp[key] !== 1) {
newArr.push(arr[index]);
temp[key] = 1;
}
}
return newArr;
};
获取cookie
/*
*funname:getCookie
*des:获取cookie
*params: [array] arr
*return: [array]
*/
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1) {
c_start = c_start + c_name.length + 1
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1) c_end = document.cookie.length
return unescape(document.cookie.substring(c_start, c_end))
}
}
return ""
}
设置cookie
/*
*funname:setCookie
*des:设置cookie
*params: [string] c_name,[string] value,expiredays
*return: [array]
*/
function setCookie(c_name, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}
检测cookie
/*
*funname:checkCookie
*des:检测cookie
*params:
*return:
*/
function checkCookie() {
username = getCookie('username')
if (username != null && username != "") {
return true;
} else {
return false;
}
}
获取localstorage
/*
*funname:getLocalStorage
*des:获取localstorage
*params: [string] key
*return: [string]
*/
function getLocalStorage(key) {
if (localStorage) {
return localStorage.getItem(key);
} else {
// 使用cookie获取
return getCookie(key);
}
return '';
}
设置localstorage
/*
*funname:setLocalStorage
*des:设置localstorage
*params:
*return: null
*/
function setLocalStorage(key, value, expiredays) {
if (localStorage) {
console.log(setLocalStorage);
localStorage.setItem(key, value);
} else {
// 使用cookie保存
setCookie(key, value, expiredays);
}
}
清空localstorage
/*
*funname:clearLocalStorage
*des:清空localstorage
*params: [string] key
*return: [string]
*/
function clearLocalStorage(key) {
if (localStorage) {
localStorage.removeItem(key);
} else {
// 使用cookie获取
clearCookie(key)
}
}
解决ios滚动问题的插件
/**
* ScrollFix v0.1
* http://www.joelambert.co.uk
*
* Copyright 2011, Joe Lambert.
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
* des: 解决ios滚动问题的插件
*/
var ScrollFix = function(elem) {
// Variables to track inputs
var startY, startTopScroll;
elem = elem || document.querySelector(elem);
// If there is no element, then do nothing
if (!elem)
return;
// Handle the start of interactions
elem.addEventListener('touchstart', function(event) {
startY = event.touches[0].pageY;
startTopScroll = elem.scrollTop;
if (startTopScroll <= 0)
elem.scrollTop = 1;
if (startTopScroll + elem.offsetHeight >= elem.scrollHeight)
elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1;
}, false);
};
动态加载js
/*
*funname:loadScript
*des:动态加载js
*params: [string] filepath,[function] onloadCallback
*return: null
*/
function loadScript(filepath, onloadCallback) {
var scriptDom = document.createElement("script");
scriptDom.onload = scriptDom.onreadystatechange = onloadCallback;
scriptDom.type = "text/javascript";
scriptDom.src = filepath;
document.body.appendChild(scriptDom);
}
克隆对象
/*
*desc:针对纯 JSON 数据对象的深拷贝
*params:[object] obj
*/
var cloneObj = function(obj) {
if (obj) {
var newObj = JSON.stringify(obj)
newObj = JSON.parse(newObj)
return newObj
}
return null
};
局限性:
- 无法复制函数
- 原型链没了,对象就是object,所属的类没了。
移动端返回上一页
/*
*desc:移动端返回上一页
*params:
*/
var appGoBack = function() {
if (!url || url == '') {
window.history.go(-1);
setTimeout(function() {
// 如果不支持history.go,返回首页
location.replace('/');
}, 300);
} else {
// ios的.history.go和history.back有bug
location.replace(url);
}
return false;
}
获取随机数
/*
* funname:GetRandomNum
*params:[number]
* des:获取随机数
*return:
*/
var getRandom = function(Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
return (Min + Math.round(Rand * Range));
};
11位手机号码验证
function checkPhone(phone ){
if(/^1(3|4|5|7|8)\d{9}$/.test(phone)){
return true;
}
return false;
}
判断是否是汉字
function isChinese(str){
var reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
return reg.test(str);
}
//u4e00-u9fbf: unicode CJK(中日韩)统一表意字符。u9fa5后至u9fbf为空
//uF900-uFAFF: unicode CJK 兼容象形文字 。uFA2D后至uFAFF为空
不定期更新,看心情更新~