本文详细介绍JavaScript静态页面值传递之URL篇,能过URL进行传值.把要传递的信息接在URL上.
Post.htm
XML/HTML代码
- <inputtype="text"name="username">
- <inputtype="text"name="sex">
- <inputtype="button"value="Post">
- <scriptlanguage="javascript">
- function Post()
- {
- //单个值 Read.htm?username=baobao;
- //多全值 Read.htm?username=baobaosex=male;
- url="Read.htm?username="+escape(document.all.username.value);
- url += "sex=" + escape(document.all.sex.value);
- location.href=url;
- }
- </script>
Read.htm
XML/HTML代码
- <scriptlanguage="javascript">
- /*
- *--------------- Read.htm -----------------
- * Request[key]
- * 功能:实现ASP的取得URL字符串,Request("AAA")
- * 参数:key,字符串.
- * 实例:alert(Request["AAA"])
- *--------------- Request.htm -----------------
- */
- varurl=location.search;
- varRequest=newObject();
- if(url.indexOf("?")!=-1)
- {
- varstr=url.substr(1) //去掉?号
- strstrs= str.split("&");
- for(vari=0;i<strs.length;i++)
- {
- Request[strs[i ].split("=")[0]]=unescape(strs[ i].split("=")[1]);
- }
- }
- alert(Request["username"])
- alert(Request["sex"])
- </script><scriptlanguage="JavaScript">
- <!--
- function Request(strName)
- {
- varstrHref="www.abc.com/index.htm?a=1&b=1&c=测试测试";
- varintPos=strHref.indexOf("?");
- varstrRight=strHref.substr(intPos + 1);
- vararrTmp=strRight.split("&");
- for(vari=0; i<arrTmp.length; i++)
- {
- vararrTemp=arrTmp[i ].split("=");
- if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
- }
- return "";
- }
- alert(Request("a"));
- alert(Request("b"));
- alert(Request("c"));
- //-->
- </script>
- <script>
- String.prototype.getQuery=function(name)
- {
- varreg=newRegExp("(^|&)"+ name +"=([^&]*)(&|$)");
- varr=this.substr(this.indexOf("?")+1).match(reg);
- if (r!=null) return unescape(r[2]); return null;
- }
- varstr="www.abc.com/index.htm?a=1&b=1&c=测试测试";
- alert(str.getQuery("a"));
- alert(str.getQuery("b"));
- alert(str.getQuery("c"));
- </script>
优点: 取值方便.可以跨域.
缺点: 值长度有限制