码迷,mamicode.com
首页 > 其他好文 > 详细

常用函数封装(实时更新)

时间:2017-03-09 11:53:08      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:src   handle   调用   browser   function   body   dex   时间格式   时分秒   

//作用:日期时间格式化
//参数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 = 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;
}
}
 

常用函数封装(实时更新)

标签:src   handle   调用   browser   function   body   dex   时间格式   时分秒   

原文地址:http://www.cnblogs.com/tkpn/p/6524349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!