天天看點

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.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);

}

htm