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

时间:2012-01-20 05:03:29

标签: javascript jquery string url

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

如果URL已经有一个以及其他查询字符串 http://mysite/events/Pages/default.aspx?kwd=cancer&hos=Munster

然后我需要检查是否& hos已经在window.location.search ..并且如果已经有一个,那么将它所拥有的任何值替换为最近选择的值,而不是像这样添加它: http://mysite/events/Pages/default.aspx?kwd=cancer&hos=Munster&hos=Carmel - >这不是我想要的。一旦用户从Dropdown位置选择Carmel,我希望这是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();
   }
});

4 个答案:

答案 0 :(得分:0)

试试BBQ plugin for jQuery。如果您要使用多个查询字符串值,那么使用起来非常简单并且是最好的插件。

要获取变量,您可以使用:

$.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" );
});

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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");
};

答案 3 :(得分: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;
}