在html中接收地址栏中的参数
今天在工作中遇到一个问题,就是要在在html文件中接收地址栏中的参数,以往都是在aspx中接收参数,只要用request[参数名]就行,后来在网友的帮忙下得到解决。虽然不是自己解决,但从中也学到些东西。
window.open ("11.htm?aa="+dd);
问题就是从11.htm中接收aa的值
解决方案如下:
1.使用 window.location.search 来取得后面的参数
// param 为 参数的名称
function getParameter(param)
{
var query = window.location.search;
var iLen = param.length;
var iStart = query.indexOf(param);
if (iStart == -1)
return "";
iStart += iLen + 1;
var iEnd = query.indexOf("&", iStart);
if (iEnd == -1)
return query.substring(iStart);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return query.substring(iStart, iEnd);
}
然后调用getParameter 方法: var temp = getParameter("aa");
2.使用 window.location.href 来取得后面的参数
var url=window.location .href;
var aa=url.indexOf('=');
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
if (aa == -1)
return "";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
url=url.substring(aa+1);
window.alert(url);
两种方法其实思路是一样的,利用字符串的indexOf属性来取值。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
问题就是从11.htm中接收aa的值
解决方案如下:
1.使用 window.location.search 来取得后面的参数
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.使用 window.location.href 来取得后面的参数
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)