JavaScriptでCGIのようにlocation.searchの情報を抽出する関数。
location.searchから必要なデータを手軽に読み出せる関数です。
location.searchに変数名=Xが含まれていた場合、変数名を引数にして関数を呼び出すとXの値が返ります。
例えばlocation.searchの値が
?data1=01&data2=02&data3
なら、L_Search関数の入出力は以下のようになる。
L_Search("data1") ⇒ "01"
L_Search("data2") ⇒ "02"
L_Search("data3") ⇒ "default"
L_Search("data4") ⇒ null
L_Search = function(vars){
if(location.search.length<2)return null;
var LS_data = location.search.substr(1,location.search.length-1).split("&");
var l = 0;
for(;l<LS_data.length;l++){
if(LS_data[l].indexOf("=")>-1)LS_data[l] = LS_data[l].split("=");
else LS_data[l] = [LS_data[l],"default"];
}
for(l=0;l<LS_data.length;l++)if(LS_data[l][0]==vars)return LS_data[l][1];
return null;
}
2006/06/25
L_Search関数にさらに機能を追加したLSOperateオブジェクト。元のL_Search関数の計算量を削減し、さらにlocation.searchの値を自在に変更する機能を搭載。しかしこれが必要なければ旧式のL_Search関数で機能としては十分。modelオブジェクトは不要って人はこのページの下のほうへ。
詳細はデモへ。
/* -------------------------------------------------------------------------- */
/*
| LSOperate
*/
/* -------------------------------------------------------------------------- */
/*
* location.searchを扱うライブラリです。
*
* LSOperate( 変数名 )でlocation.searchに含まれる"変数名=値"の"値"を取り出せます。
* 変数が存在しないときはundefined、値がないときはnullが返ります。
*
* var foo = new LSOperate.model()でlocation.searchを編集できます。
* foo.valueには編集後のlocation.hrefが常に保持されます。
* foo.del( 変数名 )とすると指定された変数が削除されます。
* foo.push( 変数名,値 )とすると指定された変数が新たに追加されます。
* foo.change( 変数名,値 )とすると指定された変数の値が変更されます。
* 値を変更したいのにfoo.pushを使うと変数が2重に追加され、読み出し時は最初の変数が優先されるので注意してください。
*/
function LSOperate( name )
{
LSOperate.make();
for( var i=0 ; i<LSOperate.datalist.length ; i++ )
if( LSOperate.datalist[i][0]==name )return LSOperate.datalist[i][1];
return undefined;
}
LSOperate.datalist = null;
LSOperate.baseUri = null;
LSOperate.isMade = false;
LSOperate._date = new Date("Jun 25, 2006");
LSOperate._version = "1.0";
LSOperate.make = function()
{
if( LSOperate.isMade )return;
else LSOperate.isMade = true;
if( location.search.length<2 )
{
LSOperate.datalist = new Array();
return;
}
LSOperate.datalist = location.search.substr(1,location.search.length-1).split("&");
for( var i=0 ; i<LSOperate.datalist.length ; i++ )
if( LSOperate.datalist[i].indexOf("=")>-1 )LSOperate.datalist[i] = LSOperate.datalist[i].split("=");
else LSOperate.datalist[i] = [LSOperate.datalist[i],"null"];
LSOperate.baseUri = location.href.split("?")[0];
}
LSOperate.model = function()
{
LSOperate.make();
this._modelLS = LSOperate.datalist.LSOperateCopy();
this._modelUri = LSOperate.baseUri;
this.value;
this.compose();
}
LSOperate.model.prototype.compose = function()
{
this.value = this._modelUri + "?";
for( var i=0 ; i<this._modelLS.length ; i++ )
{
this.value += this._modelLS[i][0];
if( this._modelLS[i][1] )this.value += "=" + this._modelLS[i][1];
if( i+1<this._modelLS.length )this.value += "&";
}
}
LSOperate.model.prototype.del = function( name )
{
for( var i=0 ; i<this._modelLS.length ; i++ )
if( this._modelLS[i][0]==name )
this._modelLS = this._modelLS.LSOperateDel(i);
this.compose();
}
LSOperate.model.prototype.push = function( name,data )
{
if( !data )data = null;
this._modelLS.LSOperatePush([name,data]);
this.compose();
}
LSOperate.model.prototype.change = function( name,data )
{
if( !data )data = null;
for( var i=0 ; i<this._modelLS.length ; i++ )
if( this._modelLS[i][0]==name )this._modelLS[i][1] = data;
this.compose();
}
Array.prototype.LSOperateCopy = function()
{
var new_array = new Array( this.length );
for( var i=0 ; i<this.length ; i++ )
{
if( typeof this[i] == "object" )var now = this[i].LSOperateCopy();
else var now = this[i];
new_array[i] = now;
}
return new_array;
}
Array.prototype.LSOperateDel = function( point )
{
var first = this.slice(0,point);
var last = this.slice(point+1,this.length);
return first.concat(last);
}
Array.prototype.LSOperatePush = function( data )
{
var plus = this.length;
return this[plus] = data;
}
/* -------------------------------------------------------------------------- */
/*
| LSOperate
*/
/* -------------------------------------------------------------------------- */
/*
* location.searchを扱うライブラリです。
*
* LSOperate( 変数名 )でlocation.searchに含まれる"変数名=値"の"値"を取り出せます。
* 変数が存在しないときはundefined、値がないときはnullが返ります。
*/
function LSOperate( name )
{
LSOperate.make();
for( var i=0 ; i<LSOperate.datalist.length ; i++ )
if( LSOperate.datalist[i][0]==name )return LSOperate.datalist[i][1];
return undefined;
}
LSOperate.datalist = null;
LSOperate.baseUri = null;
LSOperate.isMade = false;
LSOperate._date = new Date("Jun 25, 2006");
LSOperate._version = "1.0";
LSOperate.make = function()
{
if( LSOperate.isMade )return;
else LSOperate.isMade = true;
if( location.search.length<2 )
{
LSOperate.datalist = new Array();
return;
}
LSOperate.datalist = location.search.substr(1,location.search.length-1).split("&");
for( var i=0 ; i<LSOperate.datalist.length ; i++ )
if( LSOperate.datalist[i].indexOf("=")>-1 )LSOperate.datalist[i] = LSOperate.datalist[i].split("=");
else LSOperate.datalist[i] = [LSOperate.datalist[i],"null"];
LSOperate.baseUri = location.href.split("?")[0];
}