Created
December 29, 2010 01:13
-
-
Save zhasm/758003 to your computer and use it in GitHub Desktop.
javascript/ jquery snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//structure | |
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
code | |
}) // document ready finished; | |
</script> | |
//on exit | |
$(window).unload(function(){ | |
}); | |
//unique array | |
Array.prototype.unique = function() { | |
var o = {}, i, l = this.length, r = []; | |
for(i=0; i<l;i++) o[this[i]] = this[i]; | |
for(i in o) r.push(o[i]); | |
return r; | |
}; | |
//variable interpolation | |
String.prototype.supplant = function (o) { | |
return this.replace(/{([^{}]*)}/g, | |
function (a, b) { | |
var r = o[b]; | |
return typeof r === 'string' || typeof r === 'number' ? r : a; | |
} | |
); | |
}; | |
//sort json key | |
function sortObject(o) { | |
var sorted = {}, | |
key, a = []; | |
for (key in o) { | |
if (o.hasOwnProperty(key)) { | |
a.push(key); | |
} | |
} | |
a.sort(); | |
for (key = 0; key < a.length; key++) { | |
sorted[a[key]] = o[a[key]]; | |
} | |
return sorted; | |
} | |
//call fanfou api; | |
$.getJSON("http://api.fanfou.com/statuses/user_timeline.json?callback=?", {id:"zhasm", count:1}, function(data){ | |
console.log(data[0].text); | |
}); | |
//format date to 1909-12-31 21:03:58 format | |
Date.prototype.format= function () { | |
var datestring="{year}-{month}-{day} {hour}:{min}:{sec}".supplant({year: | |
this.getFullYear(), | |
month:this.getMonth()+1, | |
day:this.getDate(), | |
hour:this.getHours(), | |
min:this.getMinutes(), | |
sec:this.getSeconds() | |
}); | |
return datestring.replace(/\b(\d)\b/g, "0$1"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment