How do I match string with URL in Jquery
I want to match a string in URL and redirect my page to other URL. Current URL is: http://example.com/?healing=f29c
Here is my code:
<script type="text/javascript">
jQuery( document ).ready(function($) {
var redirect_url = 'example.com/healing/';
var current_url = window.location.href;
if (current_url.indexOf('?healing=')){
if(!current_url.match(redirect_url)){
window.location.replace(redirect_url);
}
return false;
}
});
</script>
But I'am not getting the proper output. It start redirecting other pages to with or without having '?healing=' string in their URL.
And their is recurrence of URL to example http://example.com/product-category/aromafrequencies/example.com/healing/
Solution 1:
You have to do
<script type="text/javascript">
jQuery( document ).ready(function($) {
var redirect_url = 'example.com/healing/';
var current_url = window.location.href;
//please check condition
if (current_url.indexOf('?healing=') > 0){
if(!current_url.match(redirect_url)){
window.location.replace(redirect_url);
}
return false;
}
});
</script>