Skip to content

Instantly share code, notes, and snippets.

View Alex1990's full-sized avatar
🐢
Get Things Done!

Alex Chao Alex1990

🐢
Get Things Done!
View GitHub Profile
@Alex1990
Alex1990 / randStr.js
Last active August 29, 2015 14:05
A simple function can be used to generate a random string with the specified length. And, the range of the characters is configurable.
/**
* A simple function can be used to generate a random string with the specified length.
* And,the range of the characters is configurable.
*
* Default:
* len: 32
* chars: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'
*
* Note:
* The method charAt will convert its first parameter to an integer by floor.
@Alex1990
Alex1990 / scrollOffset.js
Last active August 29, 2015 14:05
Get the scroll offset of the document in a window.
/*
* Notes: window.scrollX and window.scrollY is CSSOM Editor's Draft, but IE6-8 doesn't support it.
* Date: 2014-8-30
* Ref:
* http://help.dottoro.com/ljnvjiow.php
* http://stackoverflow.com/questions/19635188/why-is-body-scrolltop-deprecated
*
* Account for zoom example: http://stackoverflow.com/questions/16618785/ie8-alternative-to-window-scrolly#answer-16618863
*/
@Alex1990
Alex1990 / cookieEnabled.js
Created August 31, 2014 06:13
Check if the cookie is enabled.
/*
* The navigator.cookieEnabled always returns true even if you've disabled the cookie in IE (test IE8).
* [Internet Options] -- [Privacy] -- [Advanced] && localhost (ie. local server, not file:///)
* So, it's not dependable.
* Ref:
* http://stackoverflow.com/questions/6125330/javascript-navigator-cookieenabled-browser-compatibility
* http://jsfiddle.net/ctRcB/
*/
function cookieEnabled() {
return ('cookie' in document) && (document.cookie.length > 0 ||
@Alex1990
Alex1990 / containDecimal.js
Created September 2, 2014 15:39
Check if a number has a decimal place.
/**
* Check if a number has a decimal place.
* Example:
* 12 --> false
* -12 --> false
* 0.12 --> true
* -0.12 --> true
* 12.12 --> true
* -12.12 --> true
* Reference:
@Alex1990
Alex1990 / escapeHtml.js
Last active July 21, 2016 03:07
[Deprecated] Please use https://github.com/component/escape-html for high performance
/**
* Escape html with no dependencies.
* Ref:
* http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery#answer-12034334
*/
var escapeMap = {
"<": "&lt;",
">": "&gt;",
"&": "&amp;",
"'": "&quot",
@Alex1990
Alex1990 / getProperty.js
Created September 5, 2014 02:03
Accessing nested object by the string.
/**
* Accessing nested object by the string (or namespace).
* Ref:
* http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key
*/
function getProperty(obj, namespace) {
var props = namespace.split(".");
for (var i = 0; i < props.length; i++) {
if (obj.hasOwnProperty(props[i])) {
obj = obj[props[i]];
@Alex1990
Alex1990 / unique.js
Last active August 29, 2015 14:06
Unique values in a array.
/**
* Unique values in a array.
*
* Ref:
* http://stackoverflow.com/questions/1960473/unique-values-in-an-array#answer-14438954
* http://stackoverflow.com/questions/1960473/unique-values-in-an-array#answer-1961068
*
* Perf:
* http://jsperf.com/distinct-hash-vs-comparison
*/
@Alex1990
Alex1990 / util-type.js
Last active August 29, 2015 14:06
Some utility function to check the object's type.
// Some utility functions to check the object's type.
// Ref:
// https://github.com/lifesinger/lifesinger.github.com/issues/175
// https://github.com/seajs/seajs/issues/1314
// Note:
// I think we should compare the compressed(min+gzip) file size.
function isType(type) {
return function(obj) {
return Object.prototype.toString.call(obj) == '[object ' + type + ']';
};
@Alex1990
Alex1990 / daysInMonth.js
Last active August 29, 2015 14:06
Get the number of days in a month in javascript.
/**
* Get the number of days in a month in javascript.
*
* Ref:
* http://stackoverflow.com/questions/315760/what-is-the-best-way-to-determine-the-number-of-days-in-a-month-with-javascript
*/
function daysInMonth(year, month) {
var now = new Date;
month = typeof month != undefined ? month
@Alex1990
Alex1990 / hasAttr.js
Created September 18, 2014 14:39
Check if a attribute exists on the element.
/**
* Check if a attribute exists on the element
*
* IE6/7 doesn't support the `hasAttribute` method
*
* Ref:
* http://codereview.stackexchange.com/questions/10131/will-this-test-to-check-if-an-element-has-an-attribute-work
*/
// Native javascript