post.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题页</title> <script language="javascript"> function Post() { //单个值 Read.htm?username=baobao; //多全值 Read.htm?username=baobao&sex=male; url = "Read.htm?username="+escape(document.all.username.value); url += "&sex=" + escape(document.all.sex.value); alert("aaa"); location.href=url; } </script> </head> <body> <input type="text" name="username" /> <input type="text" name="sex" /> <input type="button" value="Post" onclick="Post()" /> </body> </html> read.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题页</title> </head> <body> <script language="javascript"> /* *--------------- Read.htm ----------------- * Request[key] * 功能:实现ASP的取得URL字符串,Request("AAA") * 参数:key,字符串. * 实例:alert(Request["AAA"]) *--------------- Request.htm ----------------- */ var url=location.search; var Request = new Object(); if(url.indexOf("?")!=-1) { var str = url.substr(1) //去掉?号 strs = str.split("&"); for(var i=0;i<strs.length;i++) { Request[strs[i ].split("=")[0]]=unescape(strs[ i].split("=")[1]); } } alert(Request["username"]) alert(Request["sex"]) </script> <script language="javascript"> <!-- function Request(strName) { var strHref = "www.abc.com/index.htm?a=1&b=1&c=测试测试"; 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 ""; } alert(Request("a")); alert(Request("b")); alert(Request("c")); //--> </script> <script> String.prototype.getQuery = function(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = this.substr(this.indexOf("\?")+1).match(reg); if (r!=null) return unescape(r[2]); return null; } var str ="www.abc.com/index.htm?a=1&b=1&c=测试测试"; alert(str.getQuery("a")); alert(str.getQuery("b")); alert(str.getQuery("c")); </script> </body> </html> |