Skip to content

Instantly share code, notes, and snippets.

@toruta39
toruta39 / getQueryStringArgs.js
Created July 14, 2012 02:03
Get arguments in the query string
function getQueryStringArgs() {
var qs = (location.search.length > 0 ? location.search.substring(1) : "");
var args = {};
var items = qs.split("&");
var item = null,
name = null,
value = null;
@toruta39
toruta39 / typeConversion.js
Last active October 7, 2015 04:57
Quick conversions of value types
var bool = !!arg;
var str = arg + "";
var num = arg * 1;
var arr = Array.prototype.slice.call(arg, 0);
//Str <=> Arr
var arr = arg.split("");
var str = arg.join("");
@toruta39
toruta39 / contains.js
Created July 14, 2012 02:10
Detect if otherNode is contained by refNode
//Detect if otherNode is contained by refNode
function contains(refNode, otherNode) {
var node = other.Node.parerntNode;
do {
if (node === refNode) {
return true;
} else {
node = node.parentNode;
}
} while (node !== null);
@toruta39
toruta39 / client.js
Created July 14, 2012 13:37
Detect client info
//Source code on [Professional JavaScript for Web Developers]
var client = (function() {
var engine = {
//Graphic Engine
ie: 0,
gecko: 0,
webkit: 0,
khtml: 0,
@toruta39
toruta39 / searchArray.js
Created July 14, 2012 14:39
Search str in arr by either exact match or not
//Search str in arr by either exact match or not
function searchArray(str, arr, exactMatch) {
if (exactMatch) {
//Search for the exact same item with str in arr
for (var i = 0; i < arr.length; i++) {
if (str == arr[i]) {
return true;
}
}
} else {
@toruta39
toruta39 / getElementOffset.js
Created July 16, 2012 03:40
Calculate the actual offset value of any element, recursively
function getElementOffset(element, property) {
//Calculate the actual property name
property = "offset"+property[0].toUpperCase()+property.slice(1).toLowerCase();
if (property == "offsetLeft" || property == "offsetTop") {
var actualOffset = element[property];
var current = element.offsetParent;
//Look up the node tree to add up all the offset value
while (current != null) {
actualOffset += current[property];
@toruta39
toruta39 / scrollTo.js
Created July 16, 2012 04:35
Scroll page to a certain pos
//Since in Chrome, document.body.scrollTop reflects the scroll distance,
//while in IE9 and Firefox, document.documentElement.scrollTop does.
//So this will work in either of the 3 browsers to scroll to the pos you want.
function scrollTo(pos) {
if(document.body.scrollTop){
document.body.scrollTop = pos;
} else if(document.documentElement.scrollTop){
document.documentElement.scrollTop = pos;
} else {
document.body.scrollTop = document.documentElement.scrollTop = pos;
@toruta39
toruta39 / preventAdditionalMouseEvent.html
Created July 17, 2012 04:18
Prevent mouseout/mouseover event triggered when moving onto/from a child element
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Prevent additional mouse event</title>
</head>
<body>
<div id="ele" style="width: 300px; height: 300px; background-color: #0FF;">
This is the parent element.
<div style="width: 200px; height: 200px; background-color: #FFF;">
@toruta39
toruta39 / checkParent.js
Created July 17, 2012 12:29
Another way to use iterative to check if otherNode is a child element of refNode
//Inspired by jQuery, link: http://www.zhangsuoyong.com/?p=111
function checkParent(refNode, otherNode) {
var parent = otherNode.parentNode;
try {
while ( parent && parent !== refNode ) {
parent = parent.parentNode;
}
return (parent !== refNode);
} catch(e) {}
@toruta39
toruta39 / randomsort.js
Last active December 10, 2015 00:28
Randomly sort an array
function randomsort(arr) {
var i = arr.length, j, tempi, tempj;
if ( i == 0 ) return this;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
tempi = arr[i];
tempj = arr[j];
arr[i] = tempj;
arr[j] = tempi;
}