JavaScript GetVariables

akx 19.08.03 20:15

Sallii GET-muuttujien käytön JavaScriptissä.

 Tekstiversio  Arvo: 0 (3 ääntä)  Äänestä: +  -
/////////////////////////////////////////////////////////////////////
/*-------------------------------------------------------------------
  AKX GetVariables                                                   
  -------------------------------------------------------------------
    Class to get HTTP GET-style variables from the document URL.
	Usage:
	1) declare a variable as GetVariables ex.
	     vars=new GetVariables();
    2) call the variable's Init function ex.
	     vars.Init();
	3) call IsVar to check whether the named variable is found ex.
	     vars.IsVar("username");
	   IsVar returns either true or false.
	4) call GetVar to retrieve a variable.
	     alert(vars.GetVar("username"));
	   GetVar returns "" if the variable is not found.
	
	You can also iterate through the variables using
	for(i=0;i<vars.VarCount();i++)
	{
	 alert(vars.GetVarNN(i)+"="+vars.GetVarNV(i));
	}
	
	
  THIS CODE IS COPYRIGHTED AND MAY BE FREELY DISTRIBUTED AS LONG AS   
  THIS COPYRIGHT NOTICE IS ENCLOSED WITHIN THE FILE. THIS SCRIPT HAS  
  BEEN CODED BY AKX (www.theakx.tk). IF YOU WANT TO MODIFY THE  
  CODE, GO RIGHT AWAY, BUT ENSURE THAT THIS COPYRIGHT NOTICE IS ALSO  
  ENCLOSED. THANKS AND HAVE FUN. STEALING IS EV0L, AS YOU SURELY KNOW.
  
  START OF CODE
*/
function GV_Init()
{
 s=location.href;
 p=s.lastIndexOf("?");
 if(p==-1)
 {
  this.Vars=0;
  return;
 }
 vs=s.substr(p+1);
 n=0;
 end=0;
 while(!end)
 {
  e=vs.indexOf("=");
  x=vs.indexOf("&");
  if((e==-1&&x==0)||vs=="") break;  
  if(x==-1) x=vs.length;
  vr=vs.substr(0,e);
  va=vs.substring(e+1,x);
  this.Varslist[n]=vr;
  this.Values[vr]=unescape(va);
  n++;
  vs=vs.substr(x+1);
 }
 this.Vars=n;
}

function GV_Vc()
{
 return this.Vars;
}

function GV_GetVar(name)
{
 for(i=0;i<this.Vars;i++)
 {
  fn=this.Varslist[i];
  if(fn==name) return this.Values[fn];
 }
 return "";
}

function GV_IsVar(name)
{
 for(i=0;i<this.Vars;i++)
 {
  fn=this.Varslist[i];
  if(fn==name) return true;
 }
 return false;
}

function GV_GetVarPlace(name)
{
 for(i=0;i<this.Vars;i++)
 {
  fn=this.Varslist[i];
  if(fn==name) return i;
 }
 return -1;
}

/*
  be careful, in a case of duplicate names the first added takes precedence.
*/
function GV_AddVar(name,value)
{
 this.Varslist[this.Vars]=name;
 this.Values[name]=value;
 this.Vars++;
 
}

function GV_GetVarNV(n)
{
 if(this.Vars>=n) return this.Values[this.Varslist[n]];
}

function GV_GetVarNN(n)
{
 if(this.Vars>=n) return this.Varslist[n];
}

function GV_ListVars()
{
 s="";
 for(i=0;i<this.VarCount();i++)
 {
  s+=this.GetVarNN(i)+"="+this.GetVarNV(i)+"\n";
 }
 alert(s);
}

function GetVariables()
{
 this.Values=new Array();
 this.Varslist=new Array();
 this.Vars=0;
 this.Init=GV_Init;
 this.VarCount=GV_Vc;
 this.GetVar=GV_GetVar;
 this.GetVarNV=GV_GetVarNV;
 this.GetVarNN=GV_GetVarNN;  
 this.IsVar=GV_IsVar;
 this.AddVar=GV_AddVar;
 this.GetVarPlace=GV_GetVarPlace;
 this.ListVars=GV_ListVars;    
}

/*-------------------------------------------------------------------
  AKX GetVariables                                                   
  -------------------------------------------------------------------
  END OF GETVARIABLES CODE                                         */
/////////////////////////////////////////////////////////////////////