javascript 代码片段
2017-06-21
BOM相关
获取屏幕宽度
//edition:20170623
//from:js高程(p198)
//获取视口宽高
var pageWidth = window.innerWidth,
pageHeight = window.innerHeight;
if(typeof pageWidth !== "number"){
//for lt IE 9
if(document.compatMode === "CSS1Compat"){
pageWidth = document.documentElement.clientWidth;
pageWidth = document.documentElement.clientHeight;
}else{
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientHeight;
}
}
获取URL的查询字符串参数
//func: 获取location对象中的查询字符串参数
//edition: 20170623
//from: js高程(p207)
function getQueryStringArgs(){
var qs = (location.search.length > 0)?location.search.substring(1):"",
//get query string and delete the start "?"
args = {}, //to store args
items = (qs.length > 0) ? qs.split("&") : [],
item = null,
name = "",
value = "",
i = 0,
len = items.length;
for(i = 0; i < len; i++){
//add each item to args
item = items[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
args[name] = value;
}
return args;
}
插件检测
//func: 插件检测
//edition: 20170623
//from: js高程(p212) (modified)
function hasPlugin(name){
if(typeof name !== "string"){
return false;
}
var identifier = "";
switch(name.toLowerCase()){
//get the plugin's identifier in IE arrording to name
case flash:
identifier = "ShockwaveFlash.ShockwaveFlash";
break;
case quicktime:
identifier = "QuickTime.QuickTime";
break;
}
var result = hasNotIEPlugin(name);
if(!result){
result = hasIEPlugin(identifier);
}
return result;
}
function hasIEPlugin(identifier){
try{
new ActiveXObject(identifier);
return true;
}catch(ex){
return false;
}
}
function hasNotIEPlugin(name){
name = name.toLowerCase();
for(var i = 0; i < navigator.plugins.length; i++){
if(navigator.plugins[i].name.toLowerCase().indexOf(name) > 0){
return true;
}
}
return false;
}
对象是否具有某一方法
//func: 检测object对象是否具有property属性
//edition: 20170623
//from: js高程(p219) (modified)
function isHostMethod(object,property){
var t = typeof object[property];
t = t.toLowerCase();
return t === "function" ||
(!!(t === "object" && boject[property])) ||
t === "unknown";
}
获取元素相对页面的左偏移和上偏移量
//func: 获取元素相对视窗的左偏移和上偏移量
//edition: 20170628
//from: js高程(p321)
//question: 应该差一个边框宽度
function getElementLeft(element){
var actualLeft = element.offsetLeft,
current = element.offsetParent;
while(current !== null){
actualLeft += current.offsetLeft;
current = current.offsetParent;
}
return actualLeft;
}
function getElementTop(element){
var actualTop = element.offsetTop,
current = element.offsetParent;
while(current !== null){
actualTop += current.offsetTop;
current = current.offsetParent;
}
return actualTop;
}
获得视窗大小
//func: 获取视窗大小
//edition: 20170628
//from: js高程(p323)
function getViewport(){
if(document.compatMode === "BackCompat"){
return {
width: document.body.clientWidth,
height: document.body.clientHeight
};
}else{
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
};
}
}
获取元素相对视口的位置
//func: 获取元素相对视口的位置
//edition: 20170628
//from: js高程(p325)
function getBoundingClientRect(element){
var scrollTop = document.documentElement.scrollTop,
scrollLeft = document.documentElement.scrollLeft;
if(element.getBoundingClientRect){
if(typeof auguments.callee.offset !== "number"){
var temp = document.createElement("div");
temp.style.cssText = "position: absolute; left: 0; top: 0";
document.body.appendChild(temp);
arguments.callee.offset = -temp.getBoundingClientRect().top - scrollTop;
document.body.removeChild(temp);
temp = null;
}
var rect = element.getBoundingClientRect(),
offset = arguments.callee.offset;
return {
left: rect.left + offset,
right: rect.right + offset,
top: rect.top + offset,
bottom: rect.bottom + offset
};
}else{
var actualTop = getElementTop(element),
actualLeft = getElementLeft(element);
return {
left: actualLeft - scrollLeft,
rigth: actualLeft + offsetWidth - scrollLeft,
top: actualTop - scrollTop,
bottom: actualTop + offsetHeight - scrollTop
};
}
}
Date日期格式化
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2019-01-03 21:21:02.760
// (new Date()).format("yyyy-M-d h:m:s.S") ==> 2019-1-3 21:21:10.291
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
(本文完)
本作品采用知识共享署名-非商业性使用-相同方式共享
4.0 国际许可协议进行许可。