首页 > 代码库 > 常用函数封装(实时更新)
常用函数封装(实时更新)
//作用:日期时间格式化
//参数1:时间对象
//参数2:连接符
//参数3:是否返回时分秒 (true返回,false不返回时分秒)
//返回值:格式化后的时间字符串
function toFormat(date,splitStr,filed){
var year = date.getFullYear();
var month = date.getMonth() + 1;
if(month < 10){
month = "0" + month;
}
var day = date.getDate();
if(day < 10){
day = "0" + day;
}
var hour = date.getHours();
if(hour < 10){
hour = "0" + hour;
}
var minute = date.getMinutes();
if(minute < 10){
minute = "0" + minute;
}
var second = date.getSeconds();
if(second < 10){
second = "0" + second;
}
var time = year + splitStr + month + splitStr + day + " " + hour + ":" + minute + ":" + second;
if(filed){
time = year + splitStr + month + splitStr + day;
}
return time;
}
/*
调用示例:
var date = new Date();
var result = toFormat(date,"-")
*/
//获取滚动条高度
return document.documentElement.scrollTop + document.body.scrollTop;
//设置滚动条高度
document.documentElement.scrollTop = document.body.scrollTop = top;
//获取可视区高度及宽度
//不需要参数
function getWidth(){
if(window.innerWidth){
return window.innerWidth;
}
else{
if(document.compatMode == "CSS1Compat"){
return document.documentElement.clientWidth;
}
else{
return document.body.clientWidth;
}
}
}
function getHeight(){
if(window.innerHeight){
return window.innerHeight;
}
else{
if(document.compatMode == "CSS1Compat"){
return document.documentElement.clientHeight;
}
else{
return document.body.clientHeight;
}
}
}
三目运算法获得可视区高度
function getHeight(){
return window.innerHeight || document.compatMode == "CSS1Compat" ?
document.documentElement.clientHeight : document.body.clientHeight;
}
//获取所传参数
function GetRequest() {
var url = location.search;
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]]=(strs[i].split("=")[1]);
}
}
return theRequest;
}
//按需加载js文件
function loadScript(scriptUrl, callback) {
var elem, bl,
isExecuted = false; // 防止在ie9中,callback执行两次
if (scriptUrl == null) {
return;
}
elem = document.createElement(‘script‘);
if ( typeof(callback) === ‘function‘ ) {
bl = true;
}
elem.src = http://www.mamicode.com/scriptUrl;
document.getElementsByTagName(‘head‘)[0].appendChild(elem);
// for ie
function handle(){
var status = elem.readyState;
if (status === ‘loaded‘ || status === ‘complete‘) {
if (bl && !isExecuted) {
callback();
isExecuted = true;
}
elem.onreadystatechange = null;
}
}
elem.onreadystatechange = handle;
// for 非ie
if (bl && !isExecuted) {
elem.onload = callback;
isExecuted = true;
}
}
//判断用户的浏览设备是移动设备还是PC
//返回值为true的时候为移动端
//返回值为false时为PC端
function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
document.writeln("您的浏览设备为:");
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
return true;
} else {
return false;
}
}
常用函数封装(实时更新)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。