SPGetQueryString empty querystring &

Jan 18, 2011 at 6:08 PM

Not sure if anyone has encountered this...

if you use SPGetQueryString from a page that's url ends in & it will error out.  An example url is below (from a quick launch or tree view link to a folder in a document library)

http://<SiteName>/<subsite>/Shared%20Documents/Forms/AllItems.aspx?RootFolder=%2F<subsite>%2FShared%20Documents%2FTest1&

That little & at the end causes the SPGetQueryString function to fail during the length check.  Here...

// Get the Query String parameters and their values and return in an array
 $.fn.SPServices.SPGetQueryString = function () {
  var queryStringVals = new Object();
  var qs = location.search.substring(1, location.search.length);
  var args = qs.split("&");
  for (var i = 0;i < args.length;i++) {
   var rxQS = /^([^=]+)=(.*)/i,
   matches = rxQS.exec(args[i]);
   if (rxQS.test(location.href))
   if (matches != null)
   if (matches.length > 2) queryStringVals[matches[1]] = unescape(matches[2]).replace('+', ' ');
  }
  return queryStringVals;
 };// End of function SPGetQueryString

As you can see, as a workaround I added a check to see if the matches is null and that seems to fix the problem.  If there is a better way to fix this, please let me know.

Coordinator
Jan 19, 2011 at 1:05 PM

Thanks for the fix! These edge conditions come up from time to time, and I appreciate a fix rather than simply a report.

M.

Coordinator
Jan 19, 2011 at 1:05 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.