返回顶部

[javascript常用代码]获取URL的QueryString值

时间:2011-09-22来源:网络收集

网络上比较好的方法(使用正则表达式)

Code:
  1. Request = {   
  2. QueryString : function(item){   
  3. var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));   
  4. return svalue ? svalue[1] : svalue;   
  5. }   
  6. }   
  7. document.write( Request.QueryString("id")+"<br/>");   
  8. document.write( Request.QueryString("age") );  

 自己写的,没有使用表则表达式:

Code:
  1. function Request(a) {   
  2.     var url = location.search.replace("?""");   
  3.     var b = url.split("&");   
  4.     for (var i = 0; i < b.length; i++) {   
  5.         var c = b[i].split("=");   
  6.         if (c[0] == a) {   
  7.             return c[1];   
  8.             break;   
  9.         }   
  10.     }   
  11. }   
  12. document.write(Request("id"))  

 

编辑:Jerman
发表评论相关评论:
前端交互
前端优化及工具
DIV+CSS
CSS基础及常用代码
W3C
Actionscript/Flex
FLA源码及flash应用
Javascript/Ajax
Javascript对象(Object)
Javascript方法(Method)
Javascript属性(Attribute)
Ajax
JS插件
jQuery框架
HTML
HTML基础及探讨