0

I am trying to read all the href elements on my website and do the following:

  1. 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=456

  2. if 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:

http://myurl.com.comhttp://myurl.com/?location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc&location=/123/abc

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(…)

Community
  • 1
  • 1
JamesG
  • 1,552
  • 8
  • 39
  • 86

3 Answers3

0

Your mouseover function will trigger many many times... Just use $(document).ready to do the replacement once on pageload. So instead of

$('html').on('mouseover', 'body', function(){

use

$(document).ready(function() {
    $('a').attr('href', function() {
        return (this.href.indexOf("?") >= 0) ? this.href + "&location=/123/" : this.href + "?location=/123/";
    });
});
baao
  • 71,625
  • 17
  • 143
  • 203
0

You can replace the specific string, and if that doesn't work, add it:

$('a').each(function(){
  var href = $(this).attr('href');
  var newhref = href.replace('location=123', 'location=456');
  if (href == newhref) {
    if (href.includes('?')) {
      newhref += '&location=456'
    } else {
      newhref += '?location=456'
    }
  }
  $(this).attr('href', newhref);
});
user372495
  • 700
  • 4
  • 11
-1

You may find it useful to gather the URL & and all the query string parameters, and then work with the parameters.

    let urlParams;
    (window.onpopstate = function() {
        let match,
            pl = /\+/g, // Regex for replacing addition symbol with a space
            search = /([^&=]+)=?([^&]*)/g,
            decode = function(s) {
                return decodeURIComponent(s.replace(pl, " "));
            },
            query = window.location.search.substring(1);

        urlParams = {};
        while (match = search.exec(query))
            urlParams[decode(match[1])] = decode(match[2]);
    })();
    let myLocation =  urlParams.location  

see original post ref

Community
  • 1
  • 1
SbnSeebee
  • 64
  • 8