2012-01-19 101 views
4

如果它第一次访问该站点并且没有添加任何查询或者只有一个查询字符串连接到URL'hos',如http://hospitalsite/events/Pages/default.aspx?hos=somevalue,那么我需要应用if条件..the else条件正在fine..How我检查,看看是否有一个查询jquery检查URL是否有查询字符串

$(".hospitalDropDown").change(function(e){ 
    currentURL=$(location).attr('href'); 
    if((currentURL == 'http://hospitalsite/events/Pages/default.aspx' or (....)) { 
    window.location.href= 'http://hospitalsite/events/Pages/default.aspx'+'?hos='+$(this).val(); 
    } else { 
    window.location.href = ($(this).val() == "All Hospitals") ? 'http://hospitalsite/events/Pages/default.aspx': currentURL +'&hos='+ $(this).val(); 
    } 
}); 

回答

20

我想你会喜欢这个值:

var queryString = window.location.search; 

如果您的网址是“HTTP ://www.google.com/search?q = findme“,那么上面的queryString变量就会相等到“?q = findme”。

您可以检查是否非空查看是否有查询字符串。

0

试试这个

$(".hospitalDropDown").change(function(e){ 
     if(location.href.indexOf('?') == -1) { 
      window.location.href= 'http://hospitalsite/events/Pages/default.aspx'+'?hos='+$(this).val(); 
      } 
      else{ 
       window.location.href = ($(this).val() == "All Hospitals") ? 'http://hospitalsite/events/Pages/default.aspx': location.href +'&hos='+ $(this).val(); } 

    }); 
2

不能完全确定有关整个查询字符串,但是这将帮助您检查是否有查询字符串各个变量:

var $_GET = {}; 

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function() { 
    function decode(s) { 
     return decodeURIComponent(s.split("+").join(" ")); 
    } 

    $_GET[decode(arguments[1])] = decode(arguments[2]); 
}); 

document.write($_GET["test"]); 

这个问题有更多一些答案可能会指向正确的方向:

how to get GET and POST variables with JQuery?

0

这里是JavaScript代码来获取查询字符串:

function getParameterByName(name) { 
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
var regexS = "[\\?&]" + name + "=([^&#]*)"; 
var regex = new RegExp(regexS); 
var results = regex.exec(window.location.href); 
if (results == null) 
    return ""; 
else 
    return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

如果此功能则没有查询字符串返回null,则可以直接调用这个函数。