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

javascript - Cannot read property 'match' of undefined - Stack Overflow

programmeradmin1浏览0评论

I'm running the following:

var token = document.location.href.split('?s=')[1].match(/[a-z0-9]+/);
var longString = "?s=" + token + "?_sft_category=";
var tokenB = document.location.href.split(longString)[1].match(/[a-z0-9]+/);
var attribuB = "." + tokenB;
jQuery('a[data-filter-value="' + attribuB + '"]').parent().parent().parent().find(".dropdown-toggle").html(tokenB).append(' <span class="caret"></span>');

How e I get?

Uncaught TypeError: Cannot read property 'match' of undefined

If I remove .match(/[a-z0-9]+/) i don't get any error, but I do need match ..

The URL looks like:

/?s=sky&_sft_category=ogilvy

I'm running the following:

var token = document.location.href.split('?s=')[1].match(/[a-z0-9]+/);
var longString = "?s=" + token + "?_sft_category=";
var tokenB = document.location.href.split(longString)[1].match(/[a-z0-9]+/);
var attribuB = "." + tokenB;
jQuery('a[data-filter-value="' + attribuB + '"]').parent().parent().parent().find(".dropdown-toggle").html(tokenB).append(' <span class="caret"></span>');

How e I get?

Uncaught TypeError: Cannot read property 'match' of undefined

If I remove .match(/[a-z0-9]+/) i don't get any error, but I do need match ..

The URL looks like:

http://www.example./xchanges/results/?s=sky&_sft_category=ogilvy
Share Improve this question edited Jan 28, 2015 at 13:26 Ionică Bizău 114k94 gold badges310 silver badges487 bronze badges asked Jan 28, 2015 at 13:01 rob.mrob.m 10.6k21 gold badges88 silver badges175 bronze badges 5
  • 1 the error is saying 'document.location.href.split(longString)[1]' doesnt exist. what does console.log(document.location.href.split(longString)); give you – atmd Commented Jan 28, 2015 at 13:03
  • ["example./xchanges/results/… ?s=sky&_sft_category=ogilvy – rob.m Commented Jan 28, 2015 at 13:05
  • so it does exists, it is like it doesn't exist if i use match – rob.m Commented Jan 28, 2015 at 13:08
  • 2 @rob.m - no it doesn't exist, you are using ? twice in longString, change it to: var longString = "?s=" + token + "&_sft_category=";. The ? character should only be used once, at the start of the querystring. – Cyclonecode Commented Jan 28, 2015 at 13:15
  • 1 @Cyclone thanks a lot! I haven't noticed it had an &, damn! Could you insert that as an answer and I will accept it? Thank you – rob.m Commented Jan 28, 2015 at 13:18
Add a ment  | 

3 Answers 3

Reset to default 3

Probably using a library would be better here. I developed a tiny JavaScript library that work with the urls: url.js.

<script src="path/to/url.js"></script>
<script src="your-js.js"></script>

Then you can do:

var token = Url.queryString("s");
var tokenB = Url.queryString("_sft_category");

And that's it.

Note that instead of calling parent() multiple times, you can use the closest() method:

$('a[data-filter-value="' + attribuB + '"]')
   .closest("<your_container>")
   .find(".dropdown-toggle")
   .html(tokenB)
   .append(' <span class="caret"></span>')
   ;

Your location.href probably doesn't contain your longString. If you split the href using a string that's not present in href, you're left with an array with only 1 string, so you cannot get the [1] element. It all depends on what your token variable is....

You've used a '?' character twice, which is incorrect. I'd use

var tokenB = document.location.href.split('_sft_category=')[1].match(/[a-z0-9]+/);

to get the desired value, as _sft_category= will probably only occur once in location.href and you should then be able to split the href into an array of 2. Saves you 2 lines of code ;) ...

You are using ? twice in longString, which in turn will make split() return an array with only one value. You need to change it to:

var longString = "?s=" + token + "&_sft_category=";

The ? character should only be used once, at the start of the querystring.

发布评论

评论列表(0)

  1. 暂无评论