0

I have the following code and I would like to make the function work on any URL containing "blog" instead of a specific URL. Please help me with the correct syntax, thanks.

window.setTimeout(function() {
  if (window.location.pathname != '/home/legal-documentation' &&
    window.location.pathname != '/home/blog/australian-business-news'
  ) {
    erOpenLoginRegisterbox(jQuery);
  }
  return false;
}, 1000);
user7637745
  • 909
  • 2
  • 13
  • 26
Rhys Jones
  • 53
  • 7
  • 1
    Feedback: rightly or wrongly, the wording of your questions matters on Stack Overflow. "Please help me with the correct syntax" strikes me as _an unresearched request for free work_. If you want to get the best out of this platform, show what you have tried, and what specific problem you had with it. – halfer Jul 01 '18 at 21:09

2 Answers2

1

What you are looking for is a Regular Expression. These are used to match a certain combination of characters, in your case, you can use String.prototype.match() in order to find Strings containing the word "blog" using this RegEx:

/blog/gi

or, in your function:

window.setTimeout(function() {
    if (window.location.pathname.match(/blog/gi)) {
        erOpenLoginRegisterbox(jQuery);
    }
    return false;
}, 1000);
Luca Kiebel
  • 9,200
  • 6
  • 29
  • 43
-1

I think you are looking for indexOf: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf It will return -1 if it does not find the string it is passed, so testing for -1 seems to correlate with your approach. Furthermore, to be on the safe side, in case you also want to exclude blog in a case-insensitive manner (in other words, you want to test for Blog, blog, BLOG, etc.), I refer to pathname as though it were uppercase (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase), and then compare it to 'BLOG'

window.setTimeout(function() {
    if (window.location.pathname.toUpperCase().indexOf('BLOG') === -1) {
        erOpenLoginRegisterbox(jQuery);
    }
    return false;
}, 1000);
Dexygen
  • 12,041
  • 11
  • 76
  • 145