0

I'm trying to run some code if a string is found in the users URL. However I only want it to run if there is nothing else following the string. The string appears at the end of the URL like this.

http://shop.com/?searchTerm=bread

My code:

if (window.location.search.indexOf('searchTerm=bread') > -1) {
do stuff;
}

This works fine but the problem is it will still run if the string is 'searchTerm=bread+rolls' I don't want this to happen. Any ideas?

I should also mention there is a bunch of other parameters in the URL that change, but the one I'm trying to target is always at the end. I'm also unable to use any libraries.

http://shop.com/?p=kjsl&g=sdmjkl&searchTerm=bread

dust
  • 131
  • 1
  • 9
  • Use a regular expression, and anchor it to the end with `$`. – Barmar Mar 12 '15 at 01:15
  • Have a look at this: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Pete Mar 12 '15 at 01:15
  • Will your url (searchTerm=bread+rolls) has more string behind e.g. searchTerm=bread+rolls&xxx=xxx? – Zesky Mar 12 '15 at 01:30
  • `window.location.search == '?searchTerm=bread'` to simple for your taste somehow …? – CBroe Mar 12 '15 at 01:33
  • @CBroe sorry I didn't mention that there are a bunch of other parameters that preceed the one I'm trying to target and they are always different. – dust Mar 12 '15 at 01:47
  • @Barmar that looks like what I need but I've no idea how to use them, I'll look into it.. – dust Mar 12 '15 at 01:48
  • @Zesky no there's no other string after what I'm trying to target but there is many parameters before. – dust Mar 12 '15 at 01:49
  • www.regular-expression.info has a tutorial on regexp. Every programmer should become familiar with them. – Barmar Mar 12 '15 at 01:50
  • Thanks I'm still a beginner, will check itout. – dust Mar 12 '15 at 01:52
  • _“no there's no other string after what I'm trying to target but there is many parameters before”_ – well then you should be able to test that with a combination of `lastIndexOf` and the string length … – CBroe Mar 12 '15 at 01:57
  • Duplicate question. Look at this [http://stackoverflow.com/questions/280634/endswith-in-javascript](http://stackoverflow.com/questions/280634/endswith-in-javascript) – TechnoCrat Mar 12 '15 at 02:22
  • There is no semantic meaning for a query parameter coming in any particular position in the list. You shouldn't be depending on that. Chances are high that something will break in the future if you do. –  Mar 12 '15 at 03:25

2 Answers2

0

You may use the following example:

var url = window.location.search;
if (/searchTerm=bread$/.test(url)) {
    do stuff;
}
else if (/searchTerm=cheese\+slices$/.test(url)) {
    do stuff;
}

$ represent end of line. \ backslash is use to escape the special character like +

Hope this help :)

Zesky
  • 524
  • 6
  • 16
0

You want String.prototype.endsWith. See MDN docs. That page also provides a polyfill. For availability, see caniuse.

However, in Chrome 41 this native implementation is 25% slower than the fastest alternative, which uses slice:

function endsWithSlice(string1, string2) {
  return string1.slice(-string2.length) === string2;
}

The regexp solution presented in another answer is 50% slower. The MDN polyfill is 38% slower, but still faster than regexp. See jsperf.