Tag Archive for function

Count # of nodes

objNode.length

source

Object Runtime Fields Evaluator

/**
* Usage:
* var o={a:1, b:'!! 1+1', c:'!! alert('c')'};
* evalObj(o);
* //o.a===1
* //o.b===2
* //o.c() will display an alert
*
* Updated to keep the scope, usage:
* var o={a:1, b:'!! this.a+1', c:'!! alert(this.b+1)'};
* evalObj.call(o,o);
* //o.a===1
* //o.b===2
* //o.c() will display an alert with 3
*
*/

function evalObj(o,rec){
if (rec === undefined) {
rec=true;
}
for (var name in o){
var elem=o[name];
if (elem){
var telem=typeof elem;
if (telem==='string' && /^!!/.test(elem)){
o[name]=new Function('return '+elem.substr(2)).call(this);
}else if (rec && telem==='object'){
evalObj.call(this,elem,rec);
}
}
}
return o;
}

source

checks for selector on current page

// function as jquery plugin to test if selector is found on the current page
(function($){ $.fn.onpage = function() { return ($(this).length>0) ? true : false } })(jQuery);

source

Convert Database/Variable Name to Human Title

<?
class inflector extends inflector_Core {
public static function titlize($rawTitle) {
if(strlen($rawTitle) < 3) {
return strtoupper($rawTitle);
} else {
return ucwords(self::humanize($rawTitle));
}
}
}

echo inflector::titlize("news_items"); // News Items
echo inflector::titlize("id"); // ID
?>

source

Function Timing Example (includes jQuery)

$(function(){
console.info("Start Test");
var d = new Date();//Get our start time
console.info(d.getSeconds() + " " + d.getMilliseconds());

//Run our test
var testBody = "";
for (var i=0; i<1000; i++){
testBody += "<div class='testable"+i+"'>";
}
$("body").append(testBody);
for (var j=0; j<1000; j++){
$(".testable"+j);
}

var d = new Date();//Get our end time
console.info(d.getSeconds() + " " + d.getMilliseconds());
console.info("End Test");
/**
* Console will now log 2 times, the difference between
* them is how long the test took to run
*/
});

source

mysql query connect function

function query($sql){
$dbhost = "localhost";
$dbuname = "root";
$dbpass = "root";
$dbname = "disco_conv";
$conxion = mysql_connect($dbhost, $dbuname, $dbpass);
$result=mysql_db_query($dbname,$sql,$conxion);
return $result;
}

source

List JQuery Functions with JQuery & Firebug

jQuery.fn.each(console.log(i));

source

Custom PHP Error Handler

function logErrorsErrorHandler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_USER_ERROR:
error_log(
"<b>My ERROR</b> [$errno] $errstr<br />
".
"  Fatal error on line $errline in file $errfile".
", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />
".
"Aborting...<br />
"
);
exit(1);
break;

case E_WARNING:
case E_USER_WARNING:
error_log("<b>My WARNING</b> [$errno] $errstr<br />
");
break;

// case E_DEPRECATED:
// case E_USER_DEPRECATED:
case E_STRICT:
break;

case E_USER_NOTICE:
error_log("<b>My NOTICE</b> [$errno] $errstr<br />
");
break;

default:
error_log("Unknown error type: [$errfile:$errline] [$errno] $errstr<br />
");
break;
}

/* Don't execute PHP internal error handler */
return true;
}

ini_set('error_log', 'error_log');
ini_set('log_errors', 'On');

set_error_handler("logErrorsErrorHandler");

source

When to use the prototype of a Function

// consider:
function Grape(type) {
this.type = type || 'Xynomavro';
}

Grape.prototype.squash = function () {
return this.type.substring(0, 3);
};

var grape = new Grape();

alert(grape.squash());

// versus:
var grapes = {
barrel : [],
squash : function (grape) {
return grape.substring(0, 3);
}
};

grapes.barrel.push('Nebbiolo');

alert(grapes.squash(grapes.barrel[0]));

source

AS3 Calling a parent function from a child movieclip

// adjust "this.parent.parent" as needed

if(this.parent.parent != null){
var parentObj:Object = this.parent.parent as Object;
parentObj.traceMe()
}

// in your parent SWF you will want, of course, a function like this:
// function traceMe():void
// {
//      trace("success");
// }

source