URL Query Vars Class

[color=blue]VERIFIED WORKING ON FIREFOX 1.8+, SAFARI 2.0+ & MSIE 6+[/color]

This may also work on older versions, however I have not verified them.

The Fix

  • Any swf that uses ExternalInterface must be embedded into the code dynamically using javascript (i.e. SWFObject / http://blog.deconcept.com/swfobject) in order to be compatible with all three browsers.

  • You are able to pass inline functions using Externalinterface.call, however in order to work the must be generic in order to work in Safari:

---- WORKS: ExternalInterface.call(“function(){return true;}”);

---- DOES NOT WORK: ExternalInterface.call(“function getVariable(){return true;}”);

Test URL
http://www.frozenflame.net/queryTest.htm?test=howdy

Below is the code to my request class based on MichaelxxOA’s urlQuery class (http://www.actionscript.org/forums/s…php3?t=119839).

* Only calls ExternalInterface to execute the javascript once.
* Uses a single Static call for variable request // Request.QueryString("varName");
* Datatypes numbers, booleans and strings.

Request.as


//External Interface Class
import flash.external.ExternalInterface;

class com.Javascript.Request
{

    //STATIC PROPERTIES
    //----------------------------------------------
    
    //Switch to Load Vars if they haven't already been loaded
    private static var hasLoaded:Boolean = false;
    
    //Define Javascript Function
    private static var getTheURL:String = "function(){return window.location.href.toString();}";
    
    //Array to store values once they have been loaded.
    private static var values:Array = new Array();



    //CONSTRUCTOR
    //----------------------------------------------
    public function Request(){}
    
    
    
    //PUBLIC STATIC METHODS
    //----------------------------------------------
    //--Query String
    public static function QueryString(varName:String)
    {
        //Pull Valuse From Query String if you havent already.
        if(!hasLoaded)
        {
            hasLoaded = true;
            getValueArray();
        }
        
        return values[varName];
    }
    
    //--Get Values From Query String
    private static function getValueArray():Void
    {
        trace("]> Loading Variables from Query String.");
        trace("   (" + getTheURL + ")");
        trace("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        
        var o:Array = new Array();
        
        //Call Javascript Function
        //ExternalInterface.call("document.write",["<script>" + "
" + "<!--" + "
" + "function getTheURL()" + "
" + "{" + "
" + "return window.location.href.toString();" + "
" + "}" + "
" + "-->" + "
" + "<\/script>"]);
        o = ExternalInterface.call(getTheURL).split("?")[1].split("&");
        
        for(var i:Number = 0; i < o.length; i ++)
        {
            var tempName:String = o*.split("=")[0];
            var tempValue:String = o*.split("=")[1];
            
            trace("  - " + tempName + " = " + tempValue);
            
            if(!isNaN(Number(tempValue)))
            {
                //Datatype value as a Number
                values[tempName] = Number(tempValue);
                
            }else if(tempValue.toLowerCase() == "true" || tempValue.toLowerCase() == "false"){
                //DataType value as a Boolean
                values[tempName] = Boolean(tempValue);
                
            }else{
                values[tempName] = tempValue;
            }
        }
        
        trace("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    }
    
}

Implementation:


var pageID:String = Request.QueryString("pageID");

Hope this helps!

Original Thread: http://www.actionscript.org/forums/showthread.php3?t=119964&highlight=URLQuery