35

I have a page with select options and I am using JQuery to refresh the page and add a string to the url when an option is clicked. Now I need a way to check the browsers url to see if it contains said string.

Looking on other threads I thought indexOf would work, but when trying this it doesn't work. How else would I check if the URL contains something like ?added-to-cart=555? The complete URL would normally look like: http://my-site.com, and after clicking one of the options it looks like this after page reload: http://my-site.com/?added-to-cart=555. I just need to check to see if the URL contains that ?added-to-cart=555 bit.

Here is what I have:

jQuery("#landing-select option").click(function(){

 $('#product-form').submit();

    window.location.href += $(this).val()

});

jQuery(document).ready(function($) {
if(window.location.indexOf("?added-to-cart=555") >= 0)
            {
                 alert("found it");
            }
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Derek
  • 4,747
  • 7
  • 44
  • 79
  • 6
    Didn't you notice the error `TypeError: window.location.indexOf is not a function` in the console? [Learn how to](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) [**debug** JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging). Reading the [MDN documentation about `window.location`](https://developer.mozilla.org/en-US/docs/Web/API/Window.location) is helpful as well. – Felix Kling Feb 12 '14 at 03:57
  • Update to `window.location.href.indexOf("string-to-match")` – Abram Jun 14 '17 at 17:39

4 Answers4

98

Use Window.location.href to take the url in javascript. it's a property that will tell you the current URL location of the browser. Setting the property to something different will redirect the page.

if (window.location.href.indexOf("?added-to-cart=555") > -1) {
    alert("found it");
}
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
  • It may be helpful to explain what " > -1" does in this scenario... it looks like adding this just checks whether or not this string exists in the window href. is there a different way to solve this? – john_h Nov 15 '17 at 19:36
  • 1
    @john_h Yes, In the javascript indexOf method returns the position of the string. If the search string is avaliable it returns greater than or equal to zero(>=0). if it is not available it just returns -1 always. – Sudharsan S Dec 21 '17 at 05:41
10

window.location is an object, not a string so you need to use window.location.href to get the actual string url

if (window.location.href.indexOf("?added-to-cart=555") >= 0) {
    alert("found it");
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
3

use href with indexof

<script type="text/javascript">
 $(document).ready(function () {
   if(window.location.href.indexOf("added-to-cart=555") > -1) {
   alert("your url contains the added-to-cart=555");
  }
});
</script>
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
3
if(window.location.href.indexOf("?added-to-cart=555") >= 0)

It's window.location.href, not window.location.

danludwig
  • 46,965
  • 25
  • 159
  • 237