最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to skip if substring is null and avoid error in console? - Stack Overflow

programmeradmin4浏览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["boFilters[Agencies]"].substring(1);
  $('.Agency .dropdown-toggle').html(ag).append(' <span class="caret"></span>');
  var cl = partDic["boFilters[Clients]"].substring(1);
  $('.Client .dropdown-toggle').html(cl).append(' <span class="caret"></span>');
  var yr = partDic["boFilters[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["boFilters[Clients]"] && partDic["boFilters[Clients]"].length>0)?partDic["boFilters[Clients]"].substring(1):'';

But I still get the same error

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["boFilters[Agencies]"].substring(1);
  $('.Agency .dropdown-toggle').html(ag).append(' <span class="caret"></span>');
  var cl = partDic["boFilters[Clients]"].substring(1);
  $('.Client .dropdown-toggle').html(cl).append(' <span class="caret"></span>');
  var yr = partDic["boFilters[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["boFilters[Clients]"] && partDic["boFilters[Clients]"].length>0)?partDic["boFilters[Clients]"].substring(1):'';

But I still get the same error

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Jan 16, 2015 at 10:20 rob.mrob.m 10.6k21 gold badges88 silver badges175 bronze badges 1
  • see : stackoverflow./questions/25973300/… – Hacketo Commented Jan 16, 2015 at 10:28
Add a ment  | 

4 Answers 4

Reset to default 6

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

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

or just safeguard it with an empty string:

var ag = (partDic["boFilters[Agencies]"] || "").substring(1);

Maybe try with things like:

var parts = (hash && hash.substring(1).split('&')) || [];

You can try to check it's type:

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

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

You can check one condition before using substring method..

if((!hash) || (!hash.substring(1)){
return false;
}
发布评论

评论列表(0)

  1. 暂无评论