2010-07-06 73 views
2

我有以下代码来查询查询字符串值并返回不同的链接,具体取决于用户选择哪个过滤器(用查询字符串重新加载页面)。如何使用JavaScript获取查询字符串,如果没有查询字符串

它工作正常,如果用户选择一个过滤器和页面重新加载。无论如何,第一次加载页面时没有查询字符串,并且javascript中断导致一些图像/链接不能显示。下面的代码

var goPage=new Array(6); 

var search = location.search; 
search = search.replace(/\?/,''); 
var searchAttributes = search.split('&'); 

for(var no=0;no<searchAttributes.length;no++){ 
    var items = searchAttributes[no].split('='); 
    eval("var "+items[0]+" = '"+items[1]+"';"); 
} 
queryString = SelectedID; 

var goPage=new Array(6); 

if (queryString == "") 
{ 
goPage[0]='https://company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Objective%20Status.aspx'; 
goPage[1]='https://company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Priorities%20Status.aspx'; 
goPage[2]='https://company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Milestone%20Status.aspx'; 
goPage[3]='https://company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Moved%20to%20Green%20Last%2030.aspx'; 
goPage[4]='https://company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Moved%20to%20Amber%20Last%2030.aspx'; 
goPage[5]='https://company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Moved%20to%20Red%20Last%2030.aspx'; 
} 

if (queryString == 25) 
{ 
goPage[0]='https://company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Objective%20Status.aspx?View={D2ADE53F-8C47-4787-80AE-6F90C84206B5}&FilterField1=Strategic_x0020_ObjectiveFilterValue1=A.%20Industry%20leadership%20through%20technical%20excellence%2C%20self%20performance%2C%20safety%20and%20environment'; 

goPage[1]='company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Priorities%20Status.aspx?SortField=Strategic_x0020_Objective&SortDir=Asc&View={9AE43100-E1E7-4705-A4C5-D8FE7326714E}&FilterField1=Strategic_x0020_Objective&FilterValue1=A.%20Industry%20leadership%20through%20technical%20excellence%2C%20self%20performance%2C%20safety%20and%20environment'; 

goPage[2]='company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Milestone%20Status.aspx?View={F9DFB655-BB78-4A66-8F65-98D37B07B9B5}&FilterField1=Strategic_x0020_Objective&FilterValue1=A.%20Industry%20leadership%20through%20technical%20excellence%2C%20self%20performance%2C%20safety%20and%20environment'; 

goPage[3]='company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Moved%20to%20Green%20Last%2030.aspx'; 

goPage[4]='company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Moved%20to%20Amber%20Last%2030.aspx?View={E2142842-9815-4FFC-9297-C0F0D8E93796}&FilterField1=Strategic_x0020_ObjectiveFilterValue1=A.%20Industry%20leadership%20through%20technical%20excellence%2C%20self%20performance%2C%20safety%20and%20environment'; 

goPage[5]='company.sharepoint.apac.microsoftonline.com/Lists/Strategic%20Items/Moved%20to%20Red%20Last%2030.aspx'; 
} 

任何关于如何改变我得到查询字符串的建议,所以如果没有JavaScript不会中断?

+0

欢迎的话,请不要忘了参观http://stackoverflow.com/faq – Reigel 2010-07-06 06:01:50

+0

可能的重复:http://stackoverflow.com/questions/128028/best-way-to-safely-read-query-string-parameters – RPM1984 2010-07-06 06:05:00

+0

你用你的代码调试过吗?萤火虫?它应该显示错误是什么。你可能只需要测试'location.search'是'undefined'还是'null'。 – 2010-07-06 06:05:40

回答

4

你可以使用这样的事情:

GetQueryStringValue:function(url, name) 
{  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
    var regexS = "[\\?&]"+name+"=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(url); 
    if(results == null) 
     return undefined;  
    return results[1];  
}; 

,那么你可以这样做:

var SelectedID = GetQueryStringValue(window.location.href, 'SelectedID'); 
if(SelectedID) 
{ 
    if(SelectedID==25) 
    { 
     //goPage[...] 
    } 
} 
else 
{ 
    //SelectedID is undefined 
    //goPage[...] 
} 
+0

如果你只是想要完整的查询字符串呢?基本上所有的“?”之后在网址中。 – 2012-06-14 16:36:15

+0

@Vance Smith看看window.location对象(https://developer.mozilla.org/en/DOM/window.location),特别是'window.location.search'。有办法从URL的查询字符串中获取json对象:http://stackoverflow.com/questions/6539761/window-location-search-query-as-json – 2012-06-15 04:41:44