I am trying to read all the href elements on my website and do the following:
check if url has any parameters starting with
?location=123
or&location=123
(123 will change randomly). If it has any of those, change the location=123 to location=456if it cannot find any of of those, check if url has ?. If so add &location=456 or else add ?location=456
I found this answer: https://stackoverflow.com/a/6337376/5675125 code:
$('html').on('mouseover', 'body', function(){
// do something
$('a').attr('href', function() {
return (this.href.indexOf("?") >= 0) ? this.href + "&location=/123/" : this.href + "?location=/123/";
});
});
However, This returns this:
Which as you can see, has the original url + original url plus hundreds of parameters added.
how do i perform this check, with the regex for the 123 bit.
UPDATE:
To check if the url contains the '?'
I am using:
var link = $(this).attr('href');
if(link.indexOf("?") >= 0){
link.attr('href', 'YESiContainAQuestionMark');
} else {
link.attr('href', 'NOiDoNotContainAQuestionMark');
}
but all I am getting in the console is:
Uncaught TypeError: Cannot read property 'includes' of undefined(…)