JavaScript解析QueryString

记事本 2010-10-18 08:15:13 累计浏览 2,164 次

JavaScript解析QueryString,记录一下。

function PageQuery(q) { 
    if (q.length > 1) this.q = q.substring(1, q.length); 
    else this.q = null; 
    this.keyValuePairs = new Array(); 
    if (q) { 
        for (var i = 0; i < this.q.split("&").length; i++) { 
            this.keyValuePairs[i] = this.q.split("&")[i]; 
        } 
    } 
    this.getKeyValuePairs = function () { return this.keyValuePairs; } 
    this.getValue = function (s) { 
        for (var j = 0; j < this.keyValuePairs.length; j++) { 
            if (this.keyValuePairs[j].split("=")[0] == s) 
                return this.keyValuePairs[j].split("=")[1]; 
        } 
        return false; 
    } 
    this.getParameters = function () { 
        var a = new Array(this.getLength()); 
        for (var j = 0; j < this.keyValuePairs.length; j++) { 
            a[j] = this.keyValuePairs[j].split("=")[0]; 
        } 
        return a; 
    } 
    this.getLength = function () { return this.keyValuePairs.length; } 
} 
function queryString(key) { 
    var page = new PageQuery(window.location.search); 
    return unescape(page.getValue(key)); 
}

同分类推荐文章

  1. Embracing Playful Imperfection With The CSS `random()` Function (2026-11-14 03:10:02)
  2. Document Picture-in-Picture API与任意元素画中画实现 (2026-11-13 17:10:02)
  3. 给博客做个站内全文搜索:纯前端离线倒排索引的设计与取舍 (2026-10-03 16:40:31)

查看更多 前端 文章 →

建议继续学习

  1. JQuery实现Excel表格呈现 (累计阅读 48,735)
  2. 深入理解Javascript之执行上下文(Execution Context) (累计阅读 18,923)
  3. 从输入 URL 到页面加载完成的过程中都发生了什么事情? (累计阅读 16,397)
  4. 图片动态局部毛玻璃模糊效果的实现 (累计阅读 15,180)
  5. 天朝第二代身份证号码的验证机制 (累计阅读 15,109)
  6. HTML 5 的data-* 自定义属性 (累计阅读 14,742)
  7. 分享一个JQUERY颜色选择插件 (累计阅读 14,550)
  8. 什么是全栈工程师? (累计阅读 14,415)
  9. 快速排序(Quicksort)的Javascript实现 (累计阅读 12,104)
  10. 7 天打造前端性能监控系统 (累计阅读 11,783)