日期:2014-05-19  浏览次数:20510 次

怎么用javascript取当前页面的参数??
比如当前页面是   aaaa.aspx?id={0}&name={1}

我想用脚本
window.onload=function(){
//这里怎么写??请大家帮忙

}

------解决方案--------------------
用截取字符串
indexOf,substring函数等可以实现.
------解决方案--------------------
var str = "http://localhost/aaa.aspx?aa=1&bb=2&cc=3&dd=4afasfsf "; alert(GetQueryString(str, "aa ")); alert(GetQueryString(str, "bb ")) alert(GetQueryString(str, "cc ")) alert(GetQueryString(str, "dd ")) function GetQueryString(url,key) { var reg = new RegExp( ".*? "+ key+ "=([^&]*)?&.*?$ "+ "|.*? "+key+ "=([^&]*)?$| "); if(reg.exec(url) == url) return " "; return url.replace(reg, "$1$2 "); // return reg.exec(url)[0].Value; } //--> </SCRIPT>
------解决方案--------------------
<script language=”JavaScript”>
var urlParts = document.URL.split(“?”);
var parameterParts = urlParts[1].split(“&”);
for (i = 0; i < parameterParts.length; i++) {
var pairParts = parameterParts[i].split(“=”);
var pairName = pairParts[0];
var pairValue = pairParts[1];
document.write(pairName + “ :“ +pairValue );
}
</script>

------解决方案--------------------
var a = window.location.search //获得?id={0}&name={1}
var b = a.split( '& '); //按&分组
var id =b[0].split( '= ')[1];
var name = b[1].split( '= ')[1];

------解决方案--------------------
function getArgs()
{
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split( '& ');
for(var i=0; i <pairs.length; i++)
{
var pos = pairs[i].indexOf( '= ');
if(pos == -1)
continue;
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
args[argname] = decodeURIComponent(value);
}
return args;
}

window.onload = function()
{
var args = getArgs();
alert(args[ "id "]);
}