簡體   English   中英

使用javascript讀取/操作查詢字符串參數的最簡單方法是什么?

[英]What is the easiest way to read/manipulate query string params using javascript?

我在網上看到的例子似乎比我預期的要復雜得多(手動解析&/?/ =成對,使用正則表達式等)。 我們正在使用asp.net ajax (在客戶端參考中看不到任何內容)並且會考慮添加jQuery,如果真的有幫助的話。

我認為那里有一個更優雅的解決方案 - 到目前為止這是我發現的最好的代碼,但我希望找到更多的HttpRequest.QueryString對象(asp.net服務器端) 提前致謝,

巴蒂爾

確實有一個jQuery的QueryString插件 ,如果你願意安裝jQuery核心和插件它可能證明是有用的。

我正在使用此功能,以防我不想使用插件:

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return null;
}

看看我的帖子,因為它告訴你具體如何做到這一點:

http://seattlesoftware.wordpress.com/2008/01/16/javascript-query-string/

對於jQuery我建議jQuery BBQ:Back Button&Query Library由“Cowboy”Ben Alman提供

jQuery BBQ利用HTML5 hashchange事件來允許簡單但功能強大的可收藏的#hash歷史記錄。 此外,jQuery BBQ提供了完整的.deparam()方法,以及散列狀態管理和片段/查詢字符串解析和合並實用程序方法。

例:

// Parse URL, deserializing query string into an object.
// http://www.example.com/foo.php?a=1&b=2&c=hello#test
// search is set to ?a=1&b=2&c=hello
// myObj is set to { a:"1", b:"2", c:"hello" }
var search = window.location.search;
var myObj = $.deparam.querystring( search );
  *$(document).ready(function () {
            $("#a").click(function () {
                window.location.href = "secondpage.aspx?id='0' & name='sunil'& add='asr' & phone='1234'";
            });
        });*


**then read the query string parameters on another using split method . Here as follows:**


  *$(document).ready(function () {
            var a = decodeURI(window.location.search);
            var id = window.location.search = "id=" + $().val();
            var name = a.split("name=")[1].split("&")[0].split("'")[1];
            var phone = a.split("phone=")[1].split("&")[0].split("'")[1];
            var add = a.split("add=")[1].split("&")[0].split("'")[1];
            alert(id+','+name+','+add+','+phone); 
        });*

如果有可能遇到重復參數(例如?tag = foo&tag = bar),那么大多數庫都不夠用。 在這種情況下,您可能想要考慮我從Jan Wolter 非常全面的解析器開發的這個庫。 我添加了.plus()和.minus()函數和往返:

https://github.com/timmc/js-tools/blob/master/src/QueryString.js

使用來自prototypejs.org的String實用程序,名為toQueryParams()。

來自他們網站的示例: http//prototypejs.org/api/string/toQueryParams

“部分=博客&ID = 45'.toQueryParams();
// - > {section:'blog',id:'45'}

“部分=博客; ID = 45'.toQueryParams();
// - > {section:'blog',id:'45'}

' http://www.example.com?section=blog&id=45#comments'.toQueryParams ();
// - > {section:'blog',id:'45'}

“部分=博客&標簽= JavaScript的&標簽=原型&標簽= doc'.toQueryParams();
// - > {section:'blog',tag:['javascript','prototype','doc']}

“標記=紅寶石%20on%20rails'.toQueryParams();
// - > {tag:'ruby on rails'}

“ID = 45&raw'.toQueryParams();
// - > {id:'45',raw:undefined}

此外,您可以使用別名parseQuery()來獲得相同的結果。

window.location.search.parseQuery();

由於window.location返回一個對象,因此必須獲取該字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM