js接收网页参数

网址示例:http://localhost/test/test.htm?id=1

<script languge=javascript>  
alert(window.location.pathname);   --返回   /test/test.htm   
alert(window.location.search);     --返回   ?id=1  
alert(window.location.href);       --返回   http://localhost/test/test.htm?id=1  
</script>

---------------------------------
location对象
含有当前URL的信息.
属性
href 整个URL字符串.
protocol 含有URL第一部分的字符串,如http:
host 包含有URL中主机名:端口号部分的字符串.如//www.zzcn.net/server/
hostname 包含URL中主机名的字符串.如http://www.zzcn.net/ ;
port 包含URL中可能存在的端口号字符串.
pathname URL中"/"以后的部分.如~list/index.htm
hash "#"号(CGI参数)之后的字符串.
search "?"号(CGI参数)之后的字符串.

在HTML中用JS接收参数用到的函数
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);

return query.substring(iStart, iEnd);
}

使用的时候:var temp = getParameter("传过来的参数");

posted @ 2009-07-31 10:28  Hayden Han  阅读(437)  评论(0编辑  收藏  举报