JS小整理
禁止右键和复制
$(document).ready( function() { document.body.oncontextmenu = document.body.ondragstart = document.body.onselectstart = document.body.onbeforecopy = function() { return false; }; document.body.onselect = document.body.oncopy =document.body.onmouseup = function() { document.selection.empty(); }; });
JSON的字符串解析成JSON数据格式
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。//1、 eval() -- 计算javascript字符串 var dataObj=eval("("+data+")");//转换为json对象 alert(eval("{}"); // return undefined alert(eval("({})");// return object[Object] //2、使用Function对象来进行返回解析,典型应用就是在JQUERY中的AJAX方法下的success等对于返回数据data的解析 var json='{"name":"CJ","age":18}'; data =(new Function("","return "+json))();
js页面跳转
1、window.open()
function goOutSystem(outSystemSign){ window.open('http://jxjy.cdeledu.com/cdel_jxjy/'+outSystemSign+'.shtml'); }
2、window.location.href
<script language="JavaScript" type="text/javascript"> window.location.href="target.aspx"; </script>
3、window.navigate
<script language="javascript"> window.navigate("target.aspx"); </script>
4、window.loction.replace(注意跟第一种方式的区别)
<script language="javascript"> window.location.replace("target.aspx"); </script>
进系统默认的是1.aspx,进入2.aspx时,用window.location.replace("3.aspx");与window.location.href ("3.aspx");用户界面效果无区别,但当3.aspx调用window.history.Go(-1); window.history.back();时,使用前者的话返回方法不好用,会返回到1.aspx。
5、self.location,和下面的top.location有小小区别
<script language="JavaScript"> self.location='target.aspx'; </script>
6、top.location
<script language="javascript"> top.location='target.aspx'; </script>
7、$(window).attr('location',_ctx+"/app/interface/appLoad");
8、不推荐这种方式跳转
<script language="javascript"> alert("返回"); window.history.back(-1); </script>
9、meta方式实现跳转(content = 3 单位是秒)
<meta http-equiv=refresh content=20;URL="http://www.wyxg.com"> //2隔20秒后跳转到http://www.wyxg.com页面
区分null、0、undefined、false
typeof(undefined) == 'undefined'
typeof(null) == 'object'
typeof("") == 'string'
typeof(0) == 'number'
typeof(false) == 'boolean'
这五个值的共同点是,在if语句中做判断,都会执行false分支。当然从广义上来看,是说明这些数值都是其对应数据类型上的无效值或空值。还有这五个值作!运算,结果全为:true。
它们到String的转换关系是:
String(undefined) -> "undefined"
String(null) -> "null"
String("") -> ""
String(0) -> "0"
String(false) -> "false"
for (var i = 0; i < data.rows.length; i++) { var status = -1; //-1无意义,只是不影响状态的判断 if(typeof(data.rows[i].invoiceStatus) == 'number'){ status = data.rows[i].invoiceStatus; if (status == 0 || status == 1 || status == 3 || status == 6 || status == 7 ||status == 8 || status == 9 ||
status == 12||status == 13 || status == 14) { $("input[type='checkbox']")[i + 1].disabled = true; } } }
获取当前日期时间“yyyy-MM-dd HH:MM:SS”
//法一 function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if(month >= 1 && month <= 9) { month = "0" + month; } if(strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() +seperator2 + date.getSeconds(); document.getElementById("info1").innerHTML = currentdate; return currentdate; }
//法二 function time() { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth(); var date = now.getDate(); var hour = now.getHours(); var minite = now.getMinutes(); var second = now.getSeconds(); var seperator1 = "-"; var seperator2 = ":"; document.getElementById("info1").innerHTML = year + seperator1 + (month + 1) + seperator1 + date + " " + hour +
seperator2 + minite + seperator2 + second;
}
