...

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

beetle
12-20-2002, 10:02 PM
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 belowfunction 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// 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 :D

Note: this code should compress ok without any errors through a tool like this one (http://www.codingforums.com/showthread.php?s=&threadid=10070).

Algorithm
12-21-2002, 09:16 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum