Skip to content

Instantly share code, notes, and snippets.

View minhd's full-sized avatar

Minh Duc Nguyen minhd

  • Australian Research Data Common
  • Canberra
View GitHub Profile
@minhd
minhd / jQuery Object Creation.js
Created September 3, 2012 05:03
jquery object creation shorthand
$("<div />",
{
    id: "test",
    name: "test",
    class: "test-class",
    css: {
        width: "100px",
        height: "100px",
        backgroundColor: "#fff"
    }
@minhd
minhd / jQuery cache image.js
Created September 3, 2012 05:04
jquery quick cache img
//cache the user img
$('img').hide().appendTo('body').one('load', function() {
   console.log('Image: '+$(this).attr('src')+' is cached...');
   $(this).remove();
});
@minhd
minhd / jQuery extends sliding.js
Created September 3, 2012 05:05
jquery extend slide show/hide
jQuery.fn.extend({
slideRightShow: function(duration) {
return this.each(function() {
$(this).show('slide', {direction: 'right'}, duration);
});
},
slideLeftHide: function(duration) {
return this.each(function() {
$(this).hide('slide', {direction: 'left'}, duration);
});
@minhd
minhd / jQuery ajaxSetup.js
Created September 3, 2012 05:06
jquery ajax overwrite
$.ajaxSetup({
error: function(err) {
//do stuff when things go wrong
console.error(err);
}
});
@minhd
minhd / JSON Stringify.js
Created September 3, 2012 05:06
stringify json
// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+obj+'"';
return String(obj);
}
else {
// recurse array or object
@minhd
minhd / gist:3606835
Created September 3, 2012 05:07
jquery extend number padding
Number.prototype.pad = function (len) {
return (new Array(len+1).join("0") + this).slice(-len);
}
@minhd
minhd / gist:3606837
Created September 3, 2012 05:07
formatXML with indentation
function formatXml(xml) {
var formatted = '';
var reg = /(>)(<)(\/*)/g;
xml = xml.replace(reg, '$1\r\n$2$3');
var pad = 0;
jQuery.each(xml.split('\r\n'), function(index, node) {
var indent = 0;
if (node.match( /.+<\/\w[^>]*>$/ )) {
indent = 0;
} else if (node.match( /^<\/\w/ )) {
@minhd
minhd / gist:3606841
Created September 3, 2012 05:08
javascript htmlentities
function htmlEntities(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
@minhd
minhd / gist:3606875
Created September 3, 2012 05:15
css3 grayscale
.grayscale {
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
filter: grayscale(100%);
filter: url(grayscale.svg); /* Firefox 4+ */
filter: gray; /* IE 6-9 */
}
@minhd
minhd / JavaScript: validate email address.js
Created November 12, 2012 02:28
JavaScript: validate email address
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}