Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  178] [ 2]  / answers: 1 / hits: 139734  / 10 Years ago, wed, february 12, 2014, 12:00:00

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);
}
});

More From » jquery

 Answers
25

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);
}

[#72577] Monday, February 10, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;