單純的html頁面是無法實現跨頁面傳值的,必須依靠js來解決。下面的例子,從a頁面輸入001搜尋,b頁面顯示搜尋的結果:
a.html code
123456 |
<form method="get" action="b.htm"> <input type="text" name="uid"> <br/><br/> <input type="submit" value="Search"> <br/></form> |
b.html code
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
<head><script type="text/javascript" >function Request(strName) { var strHref = window.document.location.href; var intPos = strHref.indexOf("?"); var strRight = strHref.substr(intPos + 1); var arrTmp = strRight.split("&"); for(var i = 0; i < arrTmp.length; i++) { var arrTemp = arrTmp[i].split("="); if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1]; } return ""; }</script></head><body><table border="1" width="300"> <tr bgcolor="gray"> <td height="25">ID</td> <td>NAME</td> </tr> <tr id="div001" style="display: none;"> <td height="25">001</td> <td>Wang</td> </tr> <tr id="div002" style="display: none;"> <td height="25">002</td> <td>Zhang</td> </tr></table><script type="text/javascript" >var uidtemp = Request("uid");if(uidtemp=="001") {document.getElementById("div001").style.display="";} else {document.getElementById("div001").style.display="none";}if(uidtemp=="002") {document.getElementById("div002").style.display="";} else {document.getElementById("div002").style.display="none";} </script></body> |
**************************************************************************************
**************************************************************************************
index.htm?參數1=數值1&參數2=數值2&參數3=資料3&參數4=數值4&...... php程式員之家
靜態html檔案js讀取url參數 根據擷取html的參數值控制html頁面輸出
一、字串分割分析法。
這裡是一個擷取URL帶QUESTRING參數的JAVASCRIPT用戶端解決方案,相當於asp的request.querystring,PHP的$_GET
函數:
<Script language="javascript">
function GetRequest() {
var url = location.search; //擷取url中"?"符後的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1); phperz.com
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
</Script>
然後我們通過調用此函數擷取對應參數值:
<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var 參數1,參數2,參數3,參數N;
參數1 = Request['參數1']; php程式員站
參數2 = Request['參數2'];
參數3 = Request['參數3'];
參數N = Request['參數N'];
</Script>
以此擷取url串中所帶的同名參數
二、正則分析法。
function GetQueryString(name)
{
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}
alert(GetQueryString("參數名1"));
alert(GetQueryString("參數名2"));
alert(GetQueryString("參數名3"));
本文來自PHP程式員站,轉載請註明出處: http://www.phperz.com/web-design/javascript/011QR46201218246.html