在HTML中用JS接收参数

 

先介绍一下JS中处理URL的方法:
网址示例: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.100-tea.com/zixun/
hostname 包含URL中主机名的字符串.如http://www.100-tea.com/ ;
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("传过来的参数");
必须得xx.html?xx=xx这样的传参形式。。。。。

posted on 2010-12-20 14:36  vibratea  阅读(474)  评论(0编辑  收藏  举报