Skip to content

Instantly share code, notes, and snippets.

(function() {
var newScript, load,
firstScriptTag = document.getElementsByTagName('script')[0];
load = function load(url) {
newScript = document.createElement('script');
newScript.async = true;
newScript.src = url;
firstScriptTag.parentNode.insertBefore(newScript, firstScriptTag);
function QueryStringToJSON() {
var pairs = location.search.slice(1).split('&');
var result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
result[pair[0]] = decodeURIComponent(pair[1] || '');
});
return JSON.parse(JSON.stringify(result));
@media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and ( min--moz-device-pixel-ratio: 1.5), only screen and ( -o-min-device-pixel-ratio: 3/2), only screen and ( min-device-pixel-ratio: 1.5), only screen and (min-resolution: 192dpi) {
/* Style Rules */
}
function parseURL(url) {
var a = document.createElement('a');
//创建一个链接
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
@fordlee404
fordlee404 / eventListener.js
Created September 10, 2013 02:14
添加/移除事件
var addEvent,removeEvent;
addEvent = function(elm, evType, fn, useCapture) {
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);//DOM2.0
return true;
}else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);//IE5+
return r;
}else {
@fordlee404
fordlee404 / toggle.js
Created September 10, 2013 02:19
显示/隐藏元素
function toggle(obj) {
var el = document.getElementById(obj);
if ( el.style.display != 'none' ) {
el.style.display = 'none';
}
else {
el.style.display = '';
}
}
@fordlee404
fordlee404 / inArray.js
Created September 10, 2013 02:22
判断数组中是否有某个值
Array.prototype.inArray = function (value) {
for (var i=0,l = this.length ; i <l ; i++) {
if (this === value) {
return true;
}
}
return false;
};
@fordlee404
fordlee404 / cookie.js
Created September 10, 2013 02:34
操作cookie
@fordlee404
fordlee404 / floatNumberIsEqual.js
Created September 10, 2013 02:44
解决由浮点数精度原因造成的 1.0-0.9!==0.1 的问题
// 通过isEqual工具方法判断数值是否相等
function isEqual(number1, number2, digits){
digits = digits === undefined? 10: digits; // 默认精度为10
return number1.toFixed(digits) === number2.toFixed(digits);
}
isEqual(1.0-0.7, 0.3); // return true
// 原生扩展方式,更喜欢面向对象的风格
Number.prototype.isEqual = function(number, digits){
@fordlee404
fordlee404 / onceEventHandler.js
Created November 18, 2013 05:38
创建一次性事件
document.getElementById("myelement").addEventListener("click", handler);
// 处理器函数
function handler(e) {
// 移除处理器
e.target.removeEventListener(e.type, arguments.callee);
alert("You'll only see this once!");
}