admin管理员组文章数量:1335847
I'm using the following to create an if statement based on the last word of the url after the slash:
// Sticky
var match = location.search.match(/(\w+)$/)[0];
if (match === 'plete') {
$('p:has(audio)').addClass('sticky-child');
$('p:has(audio)').appendTo('.lesson_lang_switch');
}
The problem is, the front page doesn't have any word at the end of the URL (After the slash):
www.example/
So I get this error:
Uncaught TypeError: Cannot read property '0' of null
How can I do it so that the error doesn't appear?
I'm using the following to create an if statement based on the last word of the url after the slash:
// Sticky
var match = location.search.match(/(\w+)$/)[0];
if (match === 'plete') {
$('p:has(audio)').addClass('sticky-child');
$('p:has(audio)').appendTo('.lesson_lang_switch');
}
The problem is, the front page doesn't have any word at the end of the URL (After the slash):
www.example./
So I get this error:
Uncaught TypeError: Cannot read property '0' of null
How can I do it so that the error doesn't appear?
Share Improve this question asked Feb 27, 2015 at 9:33 wycwyc 55.4k82 gold badges256 silver badges440 bronze badges5 Answers
Reset to default 6You could add a conditional check. i.e.
var match = (location.search.match(/(\w+)$/))
? location.search.match(/(\w+)$/)[0]
: "";
if (match === 'plete') { // match will be "" if the above is false
$('p:has(audio)').addClass('sticky-child');
$('p:has(audio)').appendTo('.lesson_lang_switch');
}
You can check if the value is null
:
// Sticky
var loc = "www.example./";
var match = loc.match(/(\w+)$/) === null ? "" : loc.match(/(\w+)$/)[0];
if (match === 'plete') {
$('p:has(audio)').addClass('sticky-child');
$('p:has(audio)').appendTo('.lesson_lang_switch');
}
fiddle
You have to check if the search actually exists. You might want to do something like this:
var match = location.search ? location.search.match(/(\w+)$/)[0] : undefined;
if (match === 'plete') {
$('p:has(audio)').addClass('sticky-child');
$('p:has(audio)').appendTo('.lesson_lang_switch');
}
location.search ?
will return true if there is a value inside location.search
if it does then the regular expression will be done. Else it will get the value undefined.
The reason you got this error is because location.search
did not had any value. The regular expression returns null
. And because you are trying to read [0]
from null
you will get this error.
you could check if there is something found like this:
var match = location.search.match(/(\w+)$/);
if(match != null){
match = match[0];
if (match === 'plete') {
$('p:has(audio)').addClass('sticky-child');
$('p:has(audio)').appendTo('.lesson_lang_switch');
}
}
string.match returns an Array containing the matched results or null if there were no matches. With this being said, I suggest that you check to see what location.search.match returns before attempting to apply and index to it.
Example:
var matches = location.search.match(/(\w+)$/);
if(matches){
var match = matches[0];
if (match === 'plete') {
$('p:has(audio)').addClass('sticky-child');
$('p:has(audio)').appendTo('.lesson_lang_switch');
}
}
Please see here if you want to learn more about JavaScript's string.match method.
本文标签: How to remove this JavaScript null errorStack Overflow
版权声明:本文标题:How to remove this JavaScript null error? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742397818a2467278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论