0

Grab the substring:

var hash = document.location.hash;

// create an object to act like a dictionary to store each value indexed by its key
var partDic = {};

// remove the leading "#" and split into parts
var parts = hash.substring(1).split('&');

// If you just want the first value, whatever it is, use this.
// But be aware it's a URL so can be set to anything in any order, so this makes little sense
// var string = parts[0].split('=')[1];

// build the dictionary from each part
$.each(parts, function(i, v) {
  // do the "=" split now
  var arr = v.split("=");

  // decode to turn "%5B" back into "[" etc
  var key = decodeURIComponent(arr[0]);
  var value = decodeURIComponent(arr[1]);

  // store in our "dictionary" object
  partDic[key] = value;
});

// Set a delay to wait for content to fully load
setTimeout( function() {
  var ag = partDic["comboFilters[Agencies]"].substring(1);
  $('.Agency .dropdown-toggle').html(ag).append(' <span class="caret"></span>');
  var cl = partDic["comboFilters[Clients]"].substring(1);
  $('.Client .dropdown-toggle').html(cl).append(' <span class="caret"></span>');
  var yr = partDic["comboFilters[Years]"].substring(1).slice(1);
  $('.Year .dropdown-toggle').html(yr).append(' <span class="caret"></span>');
}, 1000);

But if there is not a substring, I am getting:

Uncaught TypeError: Cannot read property 'substring' of undefined

Suggested answer in another question

var cl = (partDic["comboFilters[Clients]"] && partDic["comboFilters[Clients]"].length>0)?partDic["comboFilters[Clients]"].substring(1):'';

But I still get the same error

Community
  • 1
  • 1
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • see : http://stackoverflow.com/questions/25973300/can-i-use-the-in-keyword-to-test-a-property-in-an-tree-object – Hacketo Jan 16 '15 at 10:28

4 Answers4

6

You can be defensive and check if a key exists before using it:

  if("comboFilters[Agencies]" in partDic) {
       var ag = partDic["comboFilters[Agencies]"].substring(1);
       $('.Agency .dropdown-toggle').html(ag).append(' <span class="caret"></span>');
  }

or just safeguard it with an empty string:

var ag = (partDic["comboFilters[Agencies]"] || "").substring(1);
georg
  • 211,518
  • 52
  • 313
  • 390
4

Maybe try with things like:

var parts = (hash && hash.substring(1).split('&')) || [];
andrusieczko
  • 2,824
  • 12
  • 23
0

You can try to check it's type:

var cl = (typeof partDic["comboFilters[Clients]"] === 'string')?partDic["comboFilters[Clients]"].substring(1):'';

Note, that you should add this check for all your variables: ag, cl, yr

antyrat
  • 27,479
  • 9
  • 75
  • 76
  • that is not creating an error but it is changing the text empty – rob.m Jan 16 '15 at 10:26
  • Correct, that means that `partDic["comboFilters[Clients]"]` type is not `string`. And `substring` method works only on strings. What does `console.log(partDic["comboFilters[Clients]"])` says? – antyrat Jan 16 '15 at 10:27
0

You can check one condition before using substring method..

if((!hash) || (!hash.substring(1)){
return false;
}
Paresh Makwana
  • 189
  • 2
  • 14