Skip to content

Instantly share code, notes, and snippets.

View 6174's full-sized avatar

Marc Chen 6174

View GitHub Profile
@6174
6174 / profile.js
Created August 7, 2013 04:02
profile a javascript functions
for (var method in jQuery.fn)(function (method) {
if (method == "init") return;
var old = jQuery.fn[method];
jQuery.fn[method] = function () {
if (!internal && curEvent) {
internal = true;
var m = curEvent.methods[curEvent.methods.length] = {
name: method,
@6174
6174 / domChangeEvent.js
Last active December 20, 2015 17:48
dom change event
//http://stackoverflow.com/questions/1449666/create-a-jquery-special-event-for-content-changed
(function(){
var interval;
jQuery.event.special.contentchange = {
setup: function(){
var self = this,
$this = $(this),
@6174
6174 / isElInDom.js
Created August 7, 2013 08:03
判断元素节点是否还在dom中
//from api
document.contains(someReferenceToADomElement);
//from job
var elementInDocument = function(element) {
while (element = element.parentNode) {
if (element == document) {
return true;
}
}
@6174
6174 / whichTransitionEvent.js
Created August 8, 2013 08:40
transitionEnd s事件兼容
function whichTransitionEvent(){
var t;
var el = document.createElement('fakeelement');
var transitions = {
'transition':'transitionend',
'OTransition':'oTransitionEnd',
'MozTransition':'transitionend',
'WebkitTransition':'webkitTransitionEnd'
}
@6174
6174 / isArrayLike.js
Created August 11, 2013 09:48
isArrayLike
function isArrayLike(obj) {
if (obj && typeof obj === "object") {
var n = obj.length
if (+n === n && !(n % 1) && n >= 0) {//检测length属性是否为非负整数
try {
if ({}.propertyIsEnumerable.call(obj, 'length') === false) {//如果是原生对象
return Array.isArray(obj) || /^\s?function/.test(obj.item || obj.callee)
}
return true;
} catch (e) {//IE的NodeList直接抛错
@6174
6174 / execCommand.html
Created August 12, 2013 05:50
execCommand兼容性测试脚本
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script>
window.onload=function (){
function getCommandList(){
return {'2D-Position':true,
'absolutePosition':true,
@6174
6174 / html-decode.js
Created August 14, 2013 08:42
html decode and encode function
function html_encode(str)
{
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, "&gt;");
s = s.replace(/</g, "&lt;");
s = s.replace(/>/g, "&gt;");
s = s.replace(/ /g, "&nbsp;");
s = s.replace(/\'/g, "&#39;");
s = s.replace(/\"/g, "&quot;");
@6174
6174 / gruntfile.js
Created August 16, 2013 01:58
a sample grunt file
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
/**
*--livescripts
*/
livescript: {
src: {
@6174
6174 / socket.io.sessionID.js
Created August 17, 2013 03:31
set session id to socket in nodejs
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
@6174
6174 / reRender.js
Last active December 21, 2015 10:19
mobile web 下面强制浏览器重渲染页面。 // 这种问题一般出现在动态生成的html当中, 如果这部分动态生成的一开始就被隐藏, 那么以后出现的时候不会被浏览器渲染。
/**
* web 浏览器有这样的一类问题:
* 当某些内容被隐藏, 过一段时间需要被显示 显示出来, 这个时候可能导致浏览器不会渲染被隐藏
* 的内容, 可以通过旋转屏幕等方式强制浏览器重新渲染页面, 下面是一个比较笨的解决方法。 求更好的解决方法
*/
function reRender(){
document.body.style.display = 'none';
setTimeout(function(){
document.body.style.display = 'block';
}, 0);