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");
}
});
This question is related to
javascript
jquery
url
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>
if(window.location.href.indexOf("?added-to-cart=555") >= 0)
It's window.location.href
, not window.location
.
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");
}
Source: Stackoverflow.com