Skip to content

Instantly share code, notes, and snippets.

@xyqfer
xyqfer / Date.prototype.toJSON.js
Created September 28, 2013 07:06
Date.prototype.toJSON
function f(n) {
return n < 10 ? '0' + n : n;
}
Date.prototype.toJSON = function () {
return isFinite(this.valueOf())
? this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
@xyqfer
xyqfer / getBoundingClientRect.js
Created September 28, 2013 07:08
getBoundingClientRect
function getElementLeft(elem) {
var left = elem.offsetLeft,
current = elem.offsetParent;
while (current) {
left += current.offsetLeft;
current = current.offsetParent;
}
return left;
@xyqfer
xyqfer / form.js
Created September 28, 2013 07:11
表单验证正则
var ruleRegex = /^(.+?)\[(.+)\]$/,
numericRegex = /^[0-9]+$/,
integerRegex = /^\-?[0-9]+$/,
decimalRegex = /^\-?[0-9]*\.?[0-9]+$/,
emailRegex = /^[a-zA-Z0-9.!#$%&amp;'*+\-\/=?\^_`{|}~\-]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*$/,
alphaRegex = /^[a-z]+$/i,
alphaNumericRegex = /^[a-z0-9]+$/i,
alphaDashRegex = /^[a-z0-9_\-]+$/i,
naturalRegex = /^[0-9]+$/i,
naturalNoZeroRegex = /^[1-9][0-9]*$/i,
@xyqfer
xyqfer / Clone.js
Created September 28, 2013 07:12
Clone an object
JSON.parse(JSON.stringify(obj));
@xyqfer
xyqfer / contain.js
Created September 28, 2013 07:13
确定HTML节点间关系
function contains(parentNode, childNode) {
if (parentNode.contains) {
return parentNode != childNode && parentNode.contains(childNode);
} else {
return !!(parentNode.compareDocumentPosition(childNode) & 16);
}
}
/*
@xyqfer
xyqfer / css3.tag.css
Created September 28, 2013 07:15
CSS3 浏览器标识
webkitProperty
MozProperty
msProperty
OProperty
property
myself.style.webkitTransform = "";
myself.style.MozTransform = "";
myself.style.msTransform = "";
myself.style.OTransform = "";
@xyqfer
xyqfer / json.ie.js
Created September 28, 2013 07:16
JSON for IE 6、7、8
var jsonStr = '{"name": "w3help", "url": "www.w3help.org", "tech": ["js", "HTML", "CSS", 5, 4.01, 2.1],'
+ '"online": true, "category": {"RCA": "Root Cause Artical", "KB":"Knowledge Base"},'
+ '"version": 1}';
json2 = (new Function("return " + jsonStr))();
for(p in json2) {
alert(json2[p]);
}
@xyqfer
xyqfer / scroll.bar.css
Created September 28, 2013 07:18
CSS设置滚动条样式
webkit:
主要有下面7个属性
::-webkit-scrollbar 滚动条整体部分,可以设置宽度
::-webkit-scrollbar-button 滚动条两端的按钮
::-webkit-scrollbar-track 外层轨道
::-webkit-scrollbar-track-piece 内层滚动槽
@xyqfer
xyqfer / trim.js
Created September 28, 2013 07:19
去除首尾空白
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+/, "").replace(/\s+$/, "");
};
}
@xyqfer
xyqfer / load.js
Created September 28, 2013 07:20
动态加载Javascript文件
var script = document.createElement("script");
if (script.readyState) {
script.onreadystatechange = function() {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
alert("ie");
}
}
} else {