Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ok, let's settle this Get Vars thing once and for all =)

    Well, some of us here have been going over and over the best way to access GET data with javascript. Well, I recently had the need to get serious with it, and ended up making a GetVars class (loosely using the term 'class') Tell me whatcha think? It's been fantastic for me. I saved this as 'get.class.js'. Of course, this contains more than some of you will need, but you can just delete the methods you don't want. The assign() and exists() are small and handy. The makeGlobal() method is extraneous, but I know some people here like it's result. I'll post some examples below
    Code:
    function GetVars( def ) 
    {
        this._def_ = def;
        var query, queries = top.location.search.substring(1).split( /\&/ );
        for ( var i=0; (query = queries[i]); i++ )
        {
            query = query.split( /\=/ );
            this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : unescape(query[1]).replace( /\+/g, " " );
        }
    }
    
    GetVars.prototype.assign = function( key )
    {
        return ( this.exists( key ) ) ? this[key] : this._def_;
    }
    
    GetVars.prototype.exists = function( key )
    {
        return ( typeof this[key] != 'undefined' );
    }
    
    GetVars.prototype.makeGlobal = function( action, prefix )
    {
        prefix = ( typeof prefix == 'undefined' ) ? '' : prefix;
        for ( var i in this )
        {
            if ( typeof i != 'function' && i != '_def_' )
            {
                var gv = prefix + i;
                if ( window[gv] ) // Global already exists
                {
                    switch( action )
                    {
                        case 0 : // Overwrite
                            window[gv] = this[i];
                            break;
                        case 1 : // Skip
                            break;
                        case 2 : // Prompt
                            if ( confirm( "The global variable '" + gv + "' aleady exists\nclick 'Ok' to overwrite, 'Cancel' to skip" ) )
                                window[gv] = this[i];
                            break;
                    }
                }
                // Global doesn't already exists, assign as is
                else window[gv] = this[i];
            }
        }
    }
    Notice what 2 does for the action parameter for makeGlobal(). Very handy to use while developing.

    Ok, examples here
    Code:
    // using querystring: ?username=peter&password=
    
    var _GET = new GetVars( null );
    var u = _GET.assign( 'username' );
    
    // u == 'peter'
    
    // _GET.exists( 'password' ) == true
    // _GET['password'] == null
    
    /*********************************/
    
    // using querystring: ?username=peter
    
    var _GET = new GetVars( null );
    alert( _GET.exists( 'password' );
    
    // alerts false
    
    /*********************************/
    
    // using querystring: ?user=beetle
    
    var user = 'guest';
    var _GET = new GetVars( null );
    _GET.makeGlobal( 0 );
    
    // user == 'beetle'
    
    /*********************************/
    
    // using querystring: ?user=beetle
    
    var user = 'guest';
    var _GET = new GetVars( null );
    _GET.makeGlobal( 0, 'new_');
    
    // user == 'guest'
    // new_user == 'beetle'
    
    /*********************************/
    
    // using querystring: ?user=beetle&pass=bailey
    
    var _GET = new GetVars( '' );
    var username = _GET.assign( 'user' );
    var password = _GET.assign( 'pass' );
    var filename = _GET.assign( 'file' );
    
    // username == 'beetle'
    // password == 'bailey'
    // filename == ''
    Comments, suggestions, improvements, errors. It's all welcome

    Note: this code should compress ok without any errors through a tool like this one.
    Last edited by beetle; 12-20-2002 at 10:10 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks good, a suggestion though: If you're going to be this robust, you should allow the ability to grab the query from window objects other than top (for when a page is in frames). It would probably also be better to grab from self by default, rather than top, for similar reasons.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •