Skip to content

Instantly share code, notes, and snippets.

View sarfraznawaz2005's full-sized avatar
🏠
Working from home

Sarfraz Ahmed sarfraznawaz2005

🏠
Working from home
View GitHub Profile
@sarfraznawaz2005
sarfraznawaz2005 / validate user input.php
Last active October 12, 2015 18:27
function to validate user input
<?php
/**
* This function can be used to check the sanity of variables
*
* @access private
*
* @param string $type The type of variable can be bool, float, numeric, string, array, or object
* @param string $string The variable name you would like to check
* @param string $length The maximum length of the variable
@sarfraznawaz2005
sarfraznawaz2005 / how to know about methods of a class.php
Last active October 12, 2015 18:28
example of how to know about methods of a class
<?php
foreach(get_class_methods(new ArrayObject()) as $key=>$method)
{
echo $key.' -> '.$method.'<br />';
}
@sarfraznawaz2005
sarfraznawaz2005 / Avoid double run.js
Last active October 12, 2015 18:29
Avoid double run
// simple way to avoid running code again when user click a button or trigger event in some other way
var shutdown = function() {
shutdown = function() {
alert("don't worry - we're already processing your shutdown request");
}
alert('This runs first!');
}
@sarfraznawaz2005
sarfraznawaz2005 / Making Cross Browser Functions.js
Last active October 12, 2015 18:29
Making Cross Browser Functions
var getMyText = function(myDiv) {
getMyText =
myDiv.innerText !== undefined ? // innerText not supported in FF
&nbsp; function(myDiv) {return myDiv.innerText} :
function(myDiv) {return myDiv.textContent};
return getMyText(myDiv);
}
@sarfraznawaz2005
sarfraznawaz2005 / Adding jQuery magic to custom objects.js
Last active October 12, 2015 18:30
Adding jQuery magic to custom objects
function Foo(n) {
this.Name = n;
}
$.extend( Foo.prototype, new jQuery.Interface, { // more prototype methods } );
// Foo preserves .constructor
@sarfraznawaz2005
sarfraznawaz2005 / Extending jQuery selector engine.js
Last active October 12, 2015 18:30
Extending jQuery selector engine
$.extend($.expr[':'],{
inline: function(a) {
return $(a).css('display') === 'inline';
}
});
// $(':inline'); // Selects ALL inline elements
@sarfraznawaz2005
sarfraznawaz2005 / Add callback to jQuery's CSS method.js
Last active October 12, 2015 18:30
Add callback to jQuery's CSS method
// Allows you to pass a function to jQuery's 'css' method
// This means you can use the -this- keyword to reference
// the current element!!!
(function($){
$.fn.oldCSSMethod = $.fn.css;
$.fn.css = function(o) {
return $.isFunction(o) ?
$(this).each(function(){
$(this).oldCSSMethod(o.call(this));
})
@sarfraznawaz2005
sarfraznawaz2005 / Closure Example.js
Last active October 12, 2015 18:30
Closure Example
/*
Defination:
Inner functions referring to local variables of its outer function create closures
OR
Declaring a function within another function, and that inner function has access to its parent function's variables, even after that parent function has returned.
@sarfraznawaz2005
sarfraznawaz2005 / Object to Array.js
Last active October 12, 2015 18:34
Object to Array
// simple way to convert an object to an array with all butter of array using Array.slice
function foo(a, b, c){
var argsArray = [].slice.apply(arguments);
argsArray.push(4,5);
alert(argsArray[4]); //5
}
foo(1, 2, 3);
@sarfraznawaz2005
sarfraznawaz2005 / String formatter.js
Last active October 12, 2015 18:30
String formatter
function format(string) {
var args = arguments;
var pattern = RegExp("%([1-" + (arguments.length-1) + "])", "g");
return string.replace(pattern, function(match, index) {
return args[index];
});
};
format("a %1 and a %2", "cat", "dog");
//"a cat and a dog"