// minified Brothercake browser detection object (thanks bro!)
// usage: alert(Browser.browser+'\n'+Browser.version+'\n'+Browser.OS)
var Browser = {
init: function () {
this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
this.version = this.searchVersion(navigator.userAgent)
|| this.searchVersion(navigator.appVersion) || "an unknown version";
this.OS = this.searchString(this.dataOS) || "an unknown OS";
},
searchString: function (data) {
for (var i=0,len=data.length;ivar dataString = data[i].string, dataProp = data[i].prop;
this.versionSearchString = data[i].versionSearch || data[i].identity;
if((dataString) && (dataString.indexOf(data[i].subString) != -1)||(dataProp)){
return data[i].identity
} } },
searchVersion: function (dataString) {
var index = dataString.indexOf(this.versionSearchString); if (index == -1) return;
return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
},
dataBrowser: [
{string: navigator.vendor, subString: "Apple", identity: "Safari"},
{prop: window.opera,identity: "Opera"},
{string: navigator.vendor, subString: "iCab", identity: "iCab"},
{string: navigator.vendor, subString: "KDE", identity: "Konqueror"},
{string: navigator.userAgent,subString: "Firefox", identity: "Firefox"},
{string: navigator.userAgent,subString: "Netscape",identity: "Netscape"},// for newer Netscapes (6+)
{string: navigator.userAgent,subString: "MSIE", identity: "IE", versionSearch: "MSIE"},
{string: navigator.userAgent,subString: "Gecko", identity: "Mozilla", versionSearch: "rv"},
{string: navigator.userAgent,subString: "Mozilla", identity: "Netscape",versionSearch: "Mozilla"}],// for older Netscapes (4-)
dataOS : [
{string: navigator.platform, subString: "Win", identity: "Windows"},
{string: navigator.platform, subString: "Mac", identity: "Mac" },
{string: navigator.platform, subString: "Linux", identity: "Linux" }]
};
Browser.init();
// IE PNG FIX: correctly handle PNG transparency in Win IE 5.5 > 6.x
if((Browser.version>5.4) && (Browser.version<7) && (Browser.browser=='IE')){
// uncomment line below and comment line above to become independent of browser detect script
//if(window.attachEvent && (parseFloat(navigator.appVersion.split("MSIE")[1])<7)){
window.attachEvent("onload",function(){ if (document.body.filters){
var i=document.images.length; while(i--){
var img = document.images[i], imgName = img.src.toUpperCase();
if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){ var
imgID = (img.id) ? "id='" + img.id + "' " : "",
imgClass = (img.className) ? "class='" + img.className + "' " : "",
imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ",
imgStyle = "display:inline-block;" + img.style.cssText;
if (img.align == "left") image.align='left'; //imgStyle = "float:left;" + imgStyle
if (img.align == "right")image.align='left'; //imgStyle = "float:right;"+ imgStyle
if (img.parentElement.href) imgStyle = "cursor:pointer;" + imgStyle
img.outerHTML= " + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\">"
} } } } ) };
document.getElementById('myBut').onclick=So far so good. Lets make a function for it to connect to. Remember, the first argument of an event is e. (IE doesn't use that, but we've made IE act like the rest now). Notice how this is our button.function doSomTing(e,h,w){
alert(this.nodeName+ " " h + w);
}
document.getElementById('myBut').onclick= doSomTing.toEvent('Hello ','World')
// Function.prototype.toEvent, .bindAsEventListener( args ) rem2006
// Important: sends event as the first argument to your function in IE also!,
// event XY [Array][x,y] is passed as last arg, your passed args are in the middle :)
// example: myElement.onclick= myFunc.toEvent( myArguments );
//
Function.prototype.toEvent = Function.prototype.bindAsEventListener= function(){
var _method= this, r=[], len=arguments.length, i=0;//alert(len)
for (i;ireturn function( ev ){ r[0]= ev= ev || event;
var db=document.body, dd=document.documentElement;
r[r.length++]=(ev.pageX)? [ev.pageX, ev.pageY] :
[ev.clientX + db.scrollLeft+ dd.scrollLeft,
ev.clientY + db.scrollTop + dd.scrollTop];
return _method.apply(ev.target|| ev.srcElement, r);
};
};
// executes string as body of function, args = comma seprated arguments
// ex.: 'var h=arguments[0]; var w=arguments[1]; alert(h+"\\n"+w)'.exe('Hello','World');
String.prototype.exe = function(){
eval( 'function(){' +this+ '}' ).apply(this,arguments);
};
String.prototype.addData= String.prototype.ad = function(n,v){
if (!window.elData){window.elData=[]}
if( !elData[this] ){ elData[this]=[] }
if( !n ){ elData[elData.length++]=this }else{ elData[this][n]=v };
return this;
};
String.prototype.getData= String.prototype.gd = function(n){
return( n )? elData[this][n] : elData[this];
};
'blah'.ad('b1','divb1id').ad('b2','divb2id');
el.id.ad(size);b1= 'blah'.gd('b1'); b2= 'blah'.gd('b1');
getSizeHistory=el.id.gd(); String.prototype.cssObj( styleObject[Object][optional] )String.prototype.cssObj= function( styleObj ){ styleObj = styleObj || {};
var pairs=this.replace( /(^\s*)|(;\s*$)/g, '' ).split( /\s*;\s*/ ),selector,propAndVal,i=pairs.length;
while( i-- ){ propAndVal= pairs[i].split( /\s*:\s*/ );
selector=propAndVal[0] ;if( selector.match(/-/) ){ selector=selector.cc() }
styleObj[ selector ]= propAndVal[1].toLowerCase()}; return styleObj;
};
String.prototype.camelCase= String.prototype.cc = function(){
return this.replace(/-\D/gi, function(match){return match.charAt(match.length - 1).toUpperCase()} );
};var s={ backgroundColor:'#f00',position:'absolute', width:'50px', height:'50px'};
'padding:5px; border-width:4px; border-style:solid; border-color:#333'.cssObj(s);/// fadeIn
this.fadeIn= function ( speed, delay ){
this.el.style.visibility='visible';
this.op(0); this._fadeIn.count = 0;
if (delay) { var _me=this;
setTimeout( function(){_me._argList[1]= speed; _me._fadeIn()}, delay*1000, _me );
} // call the fade function and returns this
else{ this._argList[1]= speed; this._fadeIn() }; return this;
};
// DOM Node Creation Helpers R.E.M. Aug.2006
// Generic Get Element, $, getEl()
// Args:[id],[element],[array]of.. Returns: [element] or [array] of elements
var $ = function getEl(e) {
if (typeof e == 'string') {return window.document.getElementById(e);}
if (e[0]) {var i=e.length; while(i--){e[i]=arguments.callee(e[i]);}return e;}
return e; //element
}
// Generic Append Element(),$A, appendElement()
// Appends [element] to [element] or [Array] of elements
var appendElement = $A =function(bag, marbles) {
if (marbles.constructor == Array){var l=marbles.length,i;
for(i=0;i else {bag.appendChild(marbles);} return bag;
}
// Generic Create Element, $X, exNihilo()
// Creates element and/or adds attributes to node and/or adds styles to node
var exNihilo = $X =function (el,attributes,styles) {
if (el==='textnode')
{ return document.createTextNode(((attributes)? attributes:' ')); }
var node=(el.constructor==String)?document.createElement(el): el,arg;
if (attributes){for (arg in attributes){node[arg]=attributes[arg];} }
if (styles) {for (arg in styles) {node.style[arg]= styles[arg];} }
return node;
}// Firefox InnerText R.E.M 2006
// Credits: Matthias Hertel , Erik Arvidsson, check by jvance
// emulates IE's innertext on Firefox's Proprietary __defineGetter__
if (typeof HTMLElement != "undefined" && typeof HTMLElement.prototype.__defineGetter__ != "undefined") {
HTMLElement.prototype.__defineGetter__("innerText", function () {
if (this.textContent) { return(this.textContent) }
else{var r=this.ownerDocument.createRange(); r.selectNodeContents(this);return r.toString();}});
HTMLElement.prototype.__defineSetter__("innerText", function (sText) {
if (this.textContent) {this.innerHTML=sText.textContent; }
else {this.innerHTML = sText.replace(/\&/g, "&").replace(//g, ">"); }} );
}
// opera innerText is broken
String.fixText=function($INPUT) {
return $INPUT.replace(/\&/g, "&").replace(/\</g, "<").replace(/\>/g, ">");
}
// Generic Create Element, $X, exNihilo() R.E.M. 2006
// Creates element and/or adds attributes to node and/or adds styles to node
var exNihilo = $X =function (el,attributes,styles) {
if (el==='textnode')
{ return document.createTextNode(((attributes)? attributes:' ')); }
var node=(el.constructor==String)?document.createElement(el): el,arg;
if (attributes){for (arg in attributes){node[arg]=attributes[arg];} }
if (styles) {for (arg in styles) {node.style[arg]= styles[arg];} }
return node;
}
var node=exNihilo( 'span',To add styles or attributes to existing nodes, pass in a node[element] instead of nodeType[string]:
{innerHTML:'hello World'},
{border:'2px solid red',padding:'20px'} )
document.body.appendChild(node);
// change/add color style of node
$X(node,0,{color:'#ff0000'})
| #TEST | FUNCTION | SECONDS |
| #1 | var _test2={}; | 1.139 µs |
| #3 | var _test2={};_test2._value=0; | 36.800 µs |
| #4 | var _test2={};_test2._value=0;_test2._value.mmm=123; | 28.275 µs |
| #5 | var _test2={_value:123}; | 30.300 µs |
| #6 | var _test2={_value:{mmm:123}}; | 1.276 µs |
| #7 | var _test2={};_test2._value={};_test2._value.mmm=123; | 1.326 µs |
| #8 | var _test2={};_test2._value={}; | 1.339 µs |
//Select text from http://www.quirksmode.org/js/selected.html
function getSel(){
if (window.getSelection){
getSel=function(){return window.getSelection() }; return getSel() }
else if (document.getSelection){
getSel=function(){return document.getSelection()};return getSel() }
else if (document.selection){
getSel=function(){return document.selection.createRange().text};return getSel() }
else {getSel=function(){return null}}
}
document.onmouseup=function(){ alert(getSel()) };
function confirmExit(){
if(!this.times) {this.times=0}
times++; if(times==2){window.onbeforeunload=''}
return 'All your sensitive information (email,posts,Google searches) '+
'is gathered by smart routers and warehoused in your namespace';
}
window.onbeforeunload= confirmExit;myFun=function(){
var me=arguments.callee;
me.browser= (document.all)
? function(){alert("Hi I'm IE")} : function(){alert("Hi I'm FireFox")};
myFun=function(myFun){
arguments.callee.caller.browser();
alert(myFun);
}
myFun(myFun);
}();
This function at first glance exhibits some strange behaviour. Not the fact that it tests for document.all ;), but the result of alert(myFun). I thought it would have been == arguments.callee.caller, like this:myFun=function(){
var me=arguments.callee;
me.browser= (document.all)
? function(){alert("Hi I'm IE")} : function(){alert("Hi I'm FireFox")};
myFun=function(myFun){
me.browser();
alert(arguments.callee.caller+"\n~~~~~~~~~~\n"+myFun);
}
myFun(me);
}();
alert(myFun);Result of alert(myFun): undefined... andResult of alert(myFun): undefined... and here is an anonymous function:
myFun=function(){
var me=arguments.callee;
me.browser= (document.all)
? function(){alert("Hi I'm IE")} : function(){alert("Hi I'm FireFox")};
myFun=function(){
me.browser();
alert(arguments.callee.caller+"\n~~~~~~~~~~\n"+myFun);
}
myFun(me);
}();
alert(myFun);
new function(){
arguments[0].message();
}({message:function(){alert('called by null')}});__=function typesNamespace(){
window.isBool = function(a){
return typeof a=='boolean';
}}();
// alert(__); = 'undefined'
// alert(typesNamespace) = 'error undefined' ???Which confused me (not hard). The author emailed me back and explained:window.isBool = ....The types namespace isn't a great example, but the reason I do this is so that I can have private variables that don't pollute the global namespace:
(function whateverNamespace(){
var AAA, BBB;
_w.someFunction = function(){
// do stuff with AAA or BBB
}
})();This way AAA and BBB don't exist under window[]."-Mark// block #A~~~~~~~~~~~~~~~~
var REM={};
(function(){
REM.b=b='boolean';//b is private
REM.isBool = function(a){
return typeof a==b;
}
})();
alert(REM.isBool + "\n"+ REM.isBool(true) + "\n" + REM.b);
//
// block #B
var REM=function(){
var b='boolean'; //b is private
return { isBool: function(a){
return typeof a==b;
}, b: b // but b is returned here
}
}();
alert(REM.isBool + "\n"+ REM.isBool(true) + "\n" + REM.b);
You like? Here is some testing code:
/*function newObject() richard maloney 2006
Args: 2+ objects
Usage:send in n objects and it'll return one object inheriting
all the prop & methods of the objects passed in */
function object() {
var o=arguments,len=o.length-1,nextObj;
while(len--){
function f() {}
f.prototype =o[0], nextO=o[len+1];
for ( var i in nextO) { f.prototype[i]=nextO[i]; }
}
return new f();
}
// setup the objects
myFruitObj= {a:'apple', b:'banana'};
myFruitObj.peel=function(){this.b='banana peel';}
myFruitObj2={c:'carrot', d:'dill pickle'};
myFruitObj3={e:'eel', f:'fish', time:function(){alert('lets eat some'+ this.e)} };
// example call to our new function in this case
// existing myFruitObj3 is overridden
myFruitObj3=object(myFruitObj,myFruitObj2,myFruitObj3);
for ( var i in myFruitObj3) {
alert(i +" : "+ myFruitObj3[i])
}
// the php file which sends a javascript file has function myFunction
// var done is to keep track of setTimeout checks
doIt=function(){
this.done=0;
var s=document.createElement("script");
s.src="http://www.XX.com/js.php";
s.type='text/javascript'; s.id='loadScript';
document.body.appendChild(s);s=null;
doIt=function(){
this.done++; //alert("this.done = " + this.done);
if (this.done>15){return;} // number of setTimeout trys before forgetting it
if (functionFromJs.php){
alert('All Good');
//do stuff
}
else {setTimeout('doIt()',333);}
}
setTimeout('doIt()',333);
}
/* create a global variable - y - that refers to an object:- */"When the exampleFuncWith function is called the resulting execution context has a scope chain consisting of its Activation object followed by the global object. The execution of the with statement adds the object referred to by the global variable y to the front of that scope chain during the evaluation of the function expression. The function object created by the evaluation of the function expression is assigned a [[scope]] property that corresponds with the scope of the execution context in which it is created. A scope chain consisting of object y followed by the Activation object from the execution context of the outer function call, followed by the global object.
var y = {x:5}; // object literal with an - x - property
function exampleFuncWith(){
var z;
/* Add the object referred to by the global variable - y - to the
front of he scope chain:-
*/
with(y){
/* evaluate a function expression to create a function object
and assign a reference to that function object to the local
variable - z - :-
*/
z = function(){alert(x);
//alert(this);
//... // inner function expression body;
}
}
return z;
}
/* execute the - exampleFuncWith - function:- */
exampleFuncWith()();
alert(x);
setInterval(function (t) { animate.call(t); }, 50, this);"Notice that the first arg is a function reference, not a string. This may not be well-known, but in combination with Function.prototype.call (or .apply) and extra trailing args to setInterval (or setTimeout), it does the job."-Brendan EichanimateEl = function(el){
var _this = this;
this.animate = function(){
setInterval(_this.animate(), 50)
}
}Function.prototype.bind = function( object )Then you can 'bind' a member function to a setTimeout call like this (assuming your object has a method named foo)"-Tom Trenka:
{
var method = this;
return function()
{
method.apply( object );
}
}
setTimeout( this.foo.bind( this ), 50 );
//taken from: http://www.crockford.com/
function object(o) {
function f() {}
f.prototype = o;
return new f();
}

Javascript Closures for Dummies
if (window.document.all) window.onunload= ... ;)Simon Willison explains: "Closures make it easy to create a memory leak without meaning to. Consider this:
function addHandler() {
var el = document.getElementById('el');
el.onclick = function() {
this.style.backgroundColor = 'red';
}
}The above code sets up the element to turn red when it is clicked. It also creates a memory leak. Why? Because the reference to el is inadvertently caught in the closure created for the anonymous inner function. This creates a circular reference between a JavaScript object (the function) and a native object (el). There are a number of workarounds for this problem. The simplest is this:function addHandler() {
var el = document.getElementById('el');
el.onclick = function() {
this.style.backgroundColor = 'red';
}
el = null;
} This works by breaking the circular reference. Surprisingly, one trick for breaking circular references introduced by a closure is to add another closure:function addHandler() {
var clickHandler = function() {
this.style.backgroundColor = 'red';
}
(function() {
var el = document.getElementById('el');
el.onclick = clickHandler;
})();
}The inner function is executed straight away, and hides its contents from the closure created with clickHandler."-©2006 A ReIntroduction to Javascript