JavaScript src attribute query


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/
1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="foo.js?val=2000&name=foo"></script>
<script type="text/javascript" src="bar.js?val=1000&name=bar"></script>




// foo.js  + bar. js source
(function(){
for(var qarr=($('script')[$('script').length-1]).src.replace(/^[^\?]+\??/,'').split('&'),
        qobj={},
        i=0,
        l=qarr.length;
        i<l;
    ++i){
qobj[qarr[i].split('=')[0]]=qarr[i].split('=')[1];
};
setInterval(function(){alert(qobj.name + ' : ' + qobj.val )},3000);
})();

Snap Value

 
function getSnapValue(value:Number,snap:*):Number {
    if (snap is Array) {
        var snap=snap.toString().split(',').sort(Array.NUMERIC);
        if (value<snap[0]) { return snap[0]; }
        if (value>snap[snap.length-1]) { return snap[snap.length-1]; }
        for (var i:uint=0,l:uint=snap.length; i<l; ++i) {
            if (snap[i]<value && snap[(i+1)]>value) { 
                return (value-snap[i]<snap[i+1]-value)?snap[i]:snap[i+1]; 
            }
        }
    } else if (snap>0) { return Math.round(value/snap)*snap; }
    return value;
}

// Example 1 - Snap Interval 
var snapInterval:Number=35; // (..,-70,-35,0,35,70,...)
trace('Snap Interval Result 1: '+getSnapValue(111,snapInterval)); // Output:  105
trace('Snap Interval Result 2: '+getSnapValue(-111,snapInterval));// Output:  -105

// Example 2 - Snap Array 
var snapValues:Array=[100,12,34,56,200,78,90,124];
trace('Snap Array Result 1: '+getSnapValue(111,snapValues)); // Output:  100
trace('Snap Array Result 2: '+getSnapValue(-111,snapValues));// Output:  12   (min value)
trace('Snap Array Result 3: '+getSnapValue(9999,snapValues));// Output:  200  (max value)

 

Array Filter

Select objects from an array nice and easy

var result:Array;
var arr:Array=[{id:'A',value:1},{id:'B',value:2},{id:'C',value:3},{id:'D',value:3}]
function getValueById(ID:String):Array { 
    return arr.filter(function(obj){return(obj.id==ID)},ID); 
}
result=getValueById('A');
trace(result.length);
trace(result[0].value);


function getIdByValue(VALUE:String):Array { 
    return arr.filter(function(obj){return(obj.value==VALUE)},VALUE); 
}
result=getIdByValue(3);
trace(result.length);
trace(result[0].id);
trace(result[1].id);

Excel Collumn Names

// Creates Excel like collumn names A-ZZ (max 702)
function getColumnNames(collumns:uint):Array{
    var c:Array=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
                 'P','Q','R','S','T','U','V','W','X','Y','Z'],	
    for(var a:Array=[],i:uint=0,j:int=0,k:uint=0,l:uint=26,p:String='';i<collumns;++i,++j){ 
	    a[i]=p+c[j]; if(j==l-1){j=-1;p=c[k];++k;} 
    }
    return a;
}
trace(getColumnNames(702))

Conditional Compilation

Config Constants allows you to control what part of your code flash should compile.
This comes in handy when targeting multiple platforms or simply to lower file size.
Here is a short example – that ought to be self-explanatory.

  1. Choose File ➔ Publish Settings. ➔ Flash tab
  2. Click the Settings button next to the value. (Script must be set to ActionScript 3.0)
  3. In the Advanced ActionScript 3.0 Settings dialog box, click the Config Constants tab.
  4. To add a constant, click the Add button.
  5. Type the name of the constant you want to add.

ActionScript:

public function conditionalCompilationTest() { 
    CONFIG::DEBUG { 
        trace('Only compile this when DEBUG=true.'); 
    } 

    CONFIG::AIR { 
        trace('Only compile this when AIR=true.'); 
    } 
}

FlashVars

A make-it-easy singleton FlashVars class

package namespace.external {
    import flash.display.LoaderInfo;
        dynamic public class FlashVars extends Object {
        private static var _instance : FlashVars = new FlashVars(SingletonLock);
	public static function get instance():FlashVars { return _instance; }
	public static function getInstance():FlashVars { return _instance; }
	public function FlashVars(lock:Class):void { 
        if(lock!= SingletonLock){
            throw new Error(this + ' Invalid Singleton Access. Use FlashVars.instance.'); 
        }
	public function parse(info:*):void { 
	    var info=(info is LoaderInfo) ? info.parameters : info;
	    for(var p in info){ this[p]=unescape(info[p].toString()); }
	}
    }
}
internal class SingletonLock {}

Usage:

import namespace.external.FlashVars

// Parse (do this once in the main swf)
FlashVars.instance.parse(root.loaderInfo);

// Accessing FlashVar "FOO"
trace(FlashVars.instance.FOO);