使用JS在URL中传递参数

aa.htm是参数输入界面
bb.htm是参数接收处理界面

aa.htm

Html代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. <script>  
  7. function submit()   
  8. {   
  9.     var input1 = document.getElementById("inputid");   
  10.     window.open("bb.html?inputStr=" + input1.value);//传入参数   
  11. }   
  12. </script>  
  13. </head>  
  14. <body>  
  15.     <input type="text" id="inputid">  
  16.     <input type="button" onclick="submit()" value="提交">  
  17. </body>  
  18. </html>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script>function submit(){ var input1 = document.getElementById("inputid"); window.open("bb.html?inputStr=" + input1.value);//传入参数}</script></head><body> <input type="text" id="inputid"> <input type="button" onclick="submit()" value="提交"></body></html>

 bb.htm

Java代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  2. <html>   
  3. <head>   
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  5. <title>Insert title here</title>   
  6. <script>   
  7. //获得参数的方法   
  8. var request =    
  9. {    
  10.     QueryString : function(val)    
  11.     {    
  12.         var uri = window.location.search;    
  13.         var re = new RegExp("" +val+ "=([^&?]*)", "ig");    
  14.         return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);    
  15.     }   
  16. }   
  17. window.onload=function(){   
  18.     var rt = request.QueryString("inputStr");   
  19.     document.getElementById("recive").value=rt;   
  20.     alert(rt);   
  21. }   
  22. </script>   
  23. </head>   
  24. <body>   
  25. 接收到:<input id="recive">   
  26. </body>   
  27. </html> 

你可能感兴趣的:(传递参数)