1.Json对象日期处理:
(Json日期格式:Date(1316756746000)/',转换后格式:yyyy-MM-dd)
function ChangeDateFormat(jsondate) {
jsondate = jsondate.replace("/Date(", "").replace(")/", "");
if (jsondate.indexOf("+") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("+"));
}
else if (jsondate.indexOf("-") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("-"));
}
var date = new Date(parseInt(jsondate, 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
if (date.getFullYear() != 1900)
return date.getFullYear() + "-" + month + "-" + currentDate;
else
return "";
}
2.获取URL地址栏上参数
function GetArgs() {
var args = new Object();
var query = location.search.substring(1); // Get query string
var pairs = query.split("&"); // Break at ampersand
for (var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('='); // Look for "name=value"
if (pos == -1)
continue; // If not found, skip
var argname = pairs[i].substring(0, pos); // Extract the name
var value = pairs[i].substring(pos + 1); // Extract the value
value = decodeURIComponent(value); // Decode it, if needed
args[argname] = value; // Store as a property
}
return args; // Return the object
}
3.是否是INT类型
function KeyIsIntNumber(KeyCode, key) {//如果输入的字符是在0-9之间,或者是backspace、DEL键
if (KeyCode == 13) { event.keyCode = 9; return true; }
if (KeyCode == 9) { event.keyCode = 9; return true; }
if (((KeyCode > 47) && (KeyCode < 58)) || ((KeyCode > 95) && (KeyCode < 106)) || (KeyCode == 8) || (KeyCode == 46) || (KeyCode > 36 && KeyCode < 41) || (KeyCode == 189)) { return true; } else { return false; }
}
4.是否是double类型
function KeyIsDoubleNumber(KeyCode, evalue) {
if (KeyCode == 13) { event.keyCode = 9; return true; }
if (KeyCode == 9) { event.keyCode = 9; return true; }
if ( (KeyCode == 190 || KeyCode == 110)) {
if (evalue.indexOf(".") > -1)
return false;
else if (evalue=="")
return false;
}
if (KeyCode == 13 || KeyCode == 46 || KeyCode == 8 || KeyCode == 110 || KeyCode == 190 || KeyCode == 189 || (KeyCode > 36 && KeyCode < 41) )
return true;
if (KeyCode < 37 || KeyCode > 40 && KeyCode < 48 || (KeyCode > 57 && KeyCode < 96) || KeyCode > 105)
return false;
else
return true;
}
function KeyIsDoubleNumber2(KeyCode, evalue) {
if (KeyCode == 13) { event.keyCode = 9; return true; }
if (KeyCode == 9) { event.keyCode = 9; return true; }
//处理负数必须是第一位输入
if ((KeyCode == 229 || KeyCode == 109)) {
if (evalue.indexOf("-") > -1)
return false;
else {//如果没有负数符号加到第一位
event.srcElement.value = "-" + evalue;
return false;
}
}
if ((KeyCode == 190 || KeyCode == 110)) {
if (evalue.indexOf(".") > -1)
return false;
else if (evalue == "")
return false;
}
if (KeyCode == 13 || KeyCode == 46 || KeyCode == 8 || KeyCode == 110 || KeyCode == 190 || KeyCode == 189 || KeyCode == 229 || KeyCode == 109 || (KeyCode > 36 && KeyCode < 41))
return true;
if (KeyCode < 37 || KeyCode > 40 && KeyCode < 48 || (KeyCode > 57 && KeyCode < 96) || KeyCode > 105)
return false;
else
return true;
}
5.时间格式验证
function DutyCheck(obj) {
var r = /^[0-9]:[0-5][0-9]$|^[0-1][0-9]:[0-5][0-9]$|^[2][0-4]:[0-5][0-9]$/;
if (obj.value != '' && !r.test(obj.value)) {
alert('时间格式不正确');
obj.value = '';
}
}
6.处理键盘事件 禁止后退键(Backspace)密码或单行、多行文本框除外
function banBackSpace(e) {
var ev = e || window.event; //获取event对象
var obj = ev.target || ev.srcElement; //获取事件源
var t = obj.type || obj.getAttribute('type'); //获取事件源类型
//获取作为判断条件的事件类型
var vReadOnly = obj.getAttribute('readonly');
var vEnabled = obj.getAttribute('enabled');
//处理null值情况
vReadOnly = (vReadOnly == null) ? false : vReadOnly;
vEnabled = (vEnabled == null) ? true : vEnabled;
//当敲Backspace键时,事件源类型为密码或单行、多行文本的,
//并且readonly属性为true或enabled属性为false的,则退格键失效
var flag1 = (ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea")
&& (vReadOnly == true || vEnabled != true)) ? true : false;
//当敲Backspace键时,事件源类型非密码或单行、多行文本的,则退格键失效
var flag2 = (ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea")
? true : false;
//判断
if (flag2) {
return false;
}
if (flag1) {
return false;
}
}
7.js文件中使用智能提示
智能提示是开发中非常实用的功能,很多软件都支持js,jquery的智能提示。不过一般都只能在页面编辑时智能提示。
有时候会遇到这种情况:我们定义了js基础库Base.js和Script1.js,在Script1.js中引用了许多Base.js中的方法,这时候在Script.js中智能提示Base.js中的成员就非常必要。
其实方法很简单:把Base.js文件往Script.js文件顶部一拖就行了,在顶部会多出这样一行代码 /// <reference path="../../../js/jquery.js" />
不管是jquery还是自定义的js文件都可以用这种方法
8.取余,取整方法
//丢弃小数部分,保留整数部分
parseInt(5/2)
//向上取整,有小数就整数部分加1
Math.ceil(5/2)
//四舍五入.
Math.round(5/2)
//向下取整
Math.floor(5/2)
9.全屏
低版本浏览器不支持(IE11)
//全屏
function activeQP(element) {
if (element.requestFullScreen) {
element.requestFullScreen();
} else if (element.webkitRequestFullScreen) {//google
element.webkitRequestFullScreen();
} else if (element.mozRequestFullScreen) {//火狐
element.mozRequestFullScreen();
}
else if (element.msRequestFullscreen) {//ie
element.msRequestFullscreen();
}
}
//取消全屏
function deActiveQP() {
// 判断浏览器种类
if (window.parent.document.exitFullscreen) {
window.parent.document.exitFullscreen();
} else if (window.parent.document.mozCancelFullScreen) {//火狐
window.parent.document.mozCancelFullScreen();
} else if (window.parent.document.webkitExitFullscreen) {//google
window.parent.document.webkitExitFullscreen();
}
else if (window.parent.document.msExitFullscreen) {//ie
window.parent.document.msExitFullscreen();
}
}
10.判断浏览器是否为ie7,8
//判断浏览器是否为IE7,8
function IsIE78() {
var browser = navigator.appName
var trim_Version;
if (browser == "Microsoft Internet Explorer") {
var b_version = navigator.appVersion
var version = b_version.split(";");
trim_Version = version[1].replace(/[ ]/g, "");
}
if (browser == "Microsoft Internet Explorer" && (trim_Version == "MSIE7.0" || trim_Version == "MSIE8.0")) {
return true;
} else {
return false;
}
}
11.字符串转json
//把字符串转换为json
function stringToJson(str) {
if (typeof (JSON) == "undefined") {
return eval("(" + str + ")");
} else {
return JSON.parse(str);
}
}
12.获取字符串的字节长度
//获取字符的字节长度
function getCharLength(char) {
return char.replace(/[\u0391-\uffe5]/g, "aa").length;
}
13.去掉空格
function trim(t) {
return t.replace(/(^\s*)|(\s*$)/g, '');
}
14.获取session值
function getMyName(){
var myName="<%=session.getAttribute("MYNAME")%>";
alert(myName);
}
15.判断一个对象是否为某个类的实例
overlays[i] instanceof MMap.Control.CustomOverlay
16.获取url参数
//方法1
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
//调用方法
alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2"));
alert(GetQueryString("参数名3"));
//方法2
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
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]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
var Request = new Object();
Request = GetRequest();
var 参数1, 参数2, 参数3, 参数N;
参数1 = Request['参数1'];
参数2 = Request['参数2'];
参数3 = Request['参数3'];
参数N = Request['参数N'];
17.获取经纬度
// chorme有时候不行,会报如下错误
//Network location provider at 'https://www.googleapis.com/' : No response received.
<script>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
var longitude = position.coords.longitude;
var latitude = position.coords.latitude;
console.log(longitude)
console.log(latitude)
},
function (e) {
var msg = e.code;
var dd = e.message;
console.log(msg)
console.log(dd)
}
)
}
</script>
18.获取ajax请求
var Ajax = {
get: function (url, fn) {
var obj = new XMLHttpRequest(); // XMLHttpRequest对象用于在后台与服务器交换数据
obj.open('GET', url, true);
obj.onreadystatechange = function () {
if (obj.readyState == 4 && obj.status == 200 || obj.status == 304) { // readyState == 4说明请求已完成
fn.call(this, obj.responseText); //从服务器获得数据
}
};
obj.send();
},
post: function (url, data, fn) { // datat应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
var obj = new XMLHttpRequest();
obj.open("POST", url, true);
obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // 添加http头,发送信息至服务器时内容编码类型
obj.onreadystatechange = function () {
if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) { // 304未修改
fn.call(this, obj.responseText);
}
};
obj.send(data);
}
}
19.加载js文件
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function (_result) {
console.log(remote_ip_info);
if (remote_ip_info.ret == '1') {
alert('国家:' + remote_ip_info.country + '\n省:' + remote_ip_info.province + '\n市:' + remote_ip_info.city + '\n区:' + remote_ip_info.district + '\nISP:' + remote_ip_info.isp + '\n类型:' + remote_ip_info.type + '\n其他:' + remote_ip_info.desc);
} else {
alert('没有找到匹配的IP地址信息!');
}
});