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 === 'complete') {
    $('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.com/
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?
You could add a conditional check. i.e.
var match = (location.search.match(/(\w+)$/)) 
    ? location.search.match(/(\w+)$/)[0] 
    : "";
 if (match === 'complete') { // match will be "" if the above is false
    $('p:has(audio)').addClass('sticky-child');
    $('p:has(audio)').appendTo('.lesson_lang_switch');
 }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With