2

I have the following code to taket the querystring value and return different links depending on which filter a user selected (that reloads the page with a querystring).

It works fine if a user selects a filter and the page reloads. Howver, the first time the page is loaded there is no querystring and the javascript breaks causing some images/links to not be shown. Code below

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';
}

Any suggestions on how to change the way I get the querystring so if there is none the javascript won't break?

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
Peter
  • 23
  • 3
  • Welcome to SO, please don't forget to visit http://stackoverflow.com/faq – Reigel Gallarde Jul 06 '10 at 06:01
  • Possible duplicate: http://stackoverflow.com/questions/128028/best-way-to-safely-read-query-string-parameters – RPM1984 Jul 06 '10 at 06:05
  • Have you debugged your code with e.g. Firebug? It should show what the error is. You probably just have to test if `location.search` is `undefined` or `null`. – Felix Kling Jul 06 '10 at 06:05
  • Hi Felix. the error is that it can't find any querystring, I have tried with some error handling to fix this (i.e. don't execute the rest of the javascript if no querystring is provided). – Peter Jul 06 '10 at 06:30
  • Dont use : `queryString = SelectedID;` You are making an unnecessary global variable here . Use `var queryString = SelectedID;` also , in JavaScript avoid using `==` instead use `===` Which line is your code breaking in ? – NM. Jul 06 '10 at 07:51

1 Answers1

4

you can use something like this:

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];      
};

then you can do this:

var SelectedID = GetQueryStringValue(window.location.href, 'SelectedID');
if(SelectedID)
{
   if(SelectedID==25)
   {
       //goPage[...]
   }
}
else
{
    //SelectedID is undefined
    //goPage[...]
}
Alex Pacurar
  • 5,801
  • 4
  • 26
  • 33
  • What if you just want the full querystring? Basically everything after the "?" in the url. – BeYourOwnGod Jun 14 '12 at 16:36
  • @Vance Smith take a look at window.location object (https://developer.mozilla.org/en/DOM/window.location) especially at 'window.location.search'. There are means to get a json object out of the url`s query string: http://stackoverflow.com/questions/6539761/window-location-search-query-as-json – Alex Pacurar Jun 15 '12 at 04:41