jquery检查URL是否已经有查询字符串

jquery check if URL has a querystring already

本文关键字:查询 字符串 检查 URL 是否 jquery      更新时间:2023-09-26

如果这是第一次加载网站,那么当用户选择医院的位置下拉列表时,它应该将hos=somelocation作为查询字符串传递到URL。http://mysite/events/Pages/default.aspx?hos=Munster

如果URL已经有一个查询字符串和其他查询字符串,如http://mysite/events/Pages/default.aspx?kwd=cancer&hos=明斯特

那么我需要检查是否&hos已经在window.location.search..中,如果已经有,则将其具有的任何值替换为最近选择的值,而不添加如下:http://mysite/events/Pages/default.aspx?kwd=cancer&hos=明斯特&hos=卡梅尔->这不是我想要的。我希望这是http://mysite/events/Pages/default.aspx?kwd=cancer&hos=一旦用户从下拉列表中选择Carmel。有人请帮忙!!

$(".LocationDropDown").change(function(e){
    var querystring=window.location.search;
    var currentURL=$(location).attr('href');
    if(querystring=='') {
        window.location.href= ( $(this).val() == "All Hospitals" ) ? 'http://mysitee/events/Pages/default.aspx':'http://mysite/events/Pages/default.aspx?hos='+$(this).val();
        }
        else            
          {
            window.location.href = ( $(this).val() == "All Hospitals" ) ? 'http://mysitee/events/Pages/default.aspx':currentURL+'&hos'+$(this).val();
   }
});

尝试jQuery的BBQ插件。如果您要像您的示例那样使用多个querystring值,那么它使用起来非常简单,是最好的插件。

要获取变量,可以使用:

$.bbq.getState( "kwd" );
$.bbq.getState( "hos" );

并设置状态:

$.bbq.pushState({ hos: Carmel }); //pushState has an optional 2nd param 
//for whether or not to delete all the params not in the push function or to keep them.

并执行URL更改功能:

$(window).bind( "hashchange", function(e) {
    // In jQuery 1.4, use e.getState( "url" );
    var url = $.bbq.getState( "url" );
});

这可能是由于在else部分使用了currentURL变量。使用http://mysite/events/Pages/default.aspx?hos='+$(this).val();而不是当前URL

我讨厌链接到网站,但我找到了这个人的解决方案,它看起来非常适合通过javascript获取和设置url参数。

http://www.west-wind.com/weblog/posts/2009/Sep/07/Get-and-Set-Querystring-Values-in-JavaScript

//check if querystring contains hos
if (getUrlEncodedKey('hos', location.search) != '') {
     //set new value
     setUrlEncodedKey('hos', newval);
}
getUrlEncodedKey = function(key, query) {
    if (!query)
        query = window.location.search;    
    var re = new RegExp("[?|&]" + key + "=(.*?)&");
    var matches = re.exec(query + "&");
    if (!matches || matches.length < 2)
        return "";
    return decodeURIComponent(matches[1].replace("+", " "));
}
setUrlEncodedKey = function(key, value, query) {
    query = query || window.location.search;
    var q = query + "&";
    var re = new RegExp("[?|&]" + key + "=.*?&");
    if (!re.test(q))
        q += key + "=" + encodeURI(value);
    else
        q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
    q = q.trimStart("&").trimEnd("&");
    return q[0]=="?" ? q : q = "?" + q;
}
//There are a couple of helpers in use here. For completeness here they are as well:    
String.prototype.trimEnd = function(c) {
    if (c)        
        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
    return this.replace(/'s+$/, '');
}
String.prototype.trimStart = function(c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^'s+/, '');
}
String.prototype.escapeRegExp = function() {
    return this.replace(/[.*+?^${}()|[']'/'']/g, "''$0");
};

只是一个代码提示,但没有经过测试。

$(".LocationDropDown").change(function(e){
    var querystring=window.location.search;
    var currentURL=$(location).attr('href');
    if(querystring=='') {
        window.location.href= ( $(this).val() == "All Hospitals" ) ? 'http://mysitee/events/Pages/default.aspx':'http://mysite/events/Pages/default.aspx?hos='+$(this).val();
    }
    else if(str.indexOf("hos=")==-1)
    {
        window.location.href = ( $(this).val() == "All Hospitals" ) ? 'http://mysitee/events/Pages/default.aspx':currentURL+'&hos='+$(this).val();
    }
    else{
        window.location.href= ( $(this).val() == "All Hospitals" ) ? 'http://mysitee/events/Pages/default.aspx': getReplacedValForHospital(currentURL, $(this).val());
    }
});
function getReplacedValForHospital(currentURL, hospitalVal) {
    var tempString = currentURL.substring(currentURL.indexOf("hos="));
    currentURL = currentURL.replace(tempString,"hos="+hospitalVal);
    return currentURL;
}