我们知道,在服务器端asp,jsp等程序可以接受html页面上的form传来的参数。那么,可不可以传递参数给html页面呢。可以。
原理:通过window.location.href中的分割符获得各个参数
方法一:
/*
*函数功能:从href获得参数
*sHref: http://www.cscenter.com.cn/arg.htm?arg1=d&arg2=re
*sArgName:arg1, arg2
*return: the value of arg. d, re
*/
function GetArgsFromHref(sHref, sArgName)
{
var args = sHref.split( " ? " );
var retval = "" ;
if (args[ 0 ] == sHref) /* 参数为空 */
{
return retval; /* 无需做任何处理 */
}
var str = args[ 1 ];
args = str.split( " & " );
for ( var i = 0 ; i < args.length; i ++ )
{
str = args[i];
var arg = str.split( " = " );
if (arg.length <= 1 ) continue ;
if (arg[ 0 ] == sArgName) retval = arg[ 1 ];
}
return retval;
}
function
getvalue(name)
{
var str = window.location.search;
if (str.indexOf(name) !=- 1 )
{
var pos_start = str.indexOf(name) + name.length + 1 ;
var pos_end = str.indexOf( " & " ,pos_start);
if (pos_end ==- 1 )
{
return str.substring(pos_start);
}
else
{
return str.substring(pos_start,pos_end)
}
}
else
{
return " 没有这个name值 " ;
}
}
alert(getvalue(name));
方法三:{
var str = window.location.search;
if (str.indexOf(name) !=- 1 )
{
var pos_start = str.indexOf(name) + name.length + 1 ;
var pos_end = str.indexOf( " & " ,pos_start);
if (pos_end ==- 1 )
{
return str.substring(pos_start);
}
else
{
return str.substring(pos_start,pos_end)
}
}
else
{
return " 没有这个name值 " ;
}
}
alert(getvalue(name));
Request
=
{
QueryString : function (item){
var svalue = location.search.match( new RegExp( " [\?\&] " + item + " =([^\&]*)(\&?) " , " i " ));
return svalue ? svalue[ 1 ] : svalue;
}
}
alert(Request.QueryString( " id " ));
方法四:QueryString : function (item){
var svalue = location.search.match( new RegExp( " [\?\&] " + item + " =([^\&]*)(\&?) " , " i " ));
return svalue ? svalue[ 1 ] : svalue;
}
}
alert(Request.QueryString( " id " ));
var
url
=
location.search;
var Request = new Object();
if (url.indexOf( " ? " ) !=- 1 )
{
var str = url.substr( 1 ); // 去掉?号
strs = str.toLowerCase();
strs = strs.split( " & " );
for ( var i = 0 ;i < strs.length;i ++ )
{
Request[strs[i].split( " = " )[ 0 ]] = unescape(strs[i].split( " = " )[ 1 ]);
}
}
var mapWidth = Request[ " w " ];
var mapHeight = Request[ " h " ];
var Request = new Object();
if (url.indexOf( " ? " ) !=- 1 )
{
var str = url.substr( 1 ); // 去掉?号
strs = str.toLowerCase();
strs = strs.split( " & " );
for ( var i = 0 ;i < strs.length;i ++ )
{
Request[strs[i].split( " = " )[ 0 ]] = unescape(strs[i].split( " = " )[ 1 ]);
}
}
var mapWidth = Request[ " w " ];
var mapHeight = Request[ " h " ];