0

so I am trying to change a value of a parameter using JS or jQuery. I have a url that looks like this:

www.exampleurl.com/somepage?foo=test

I already have the code for getting the value coming after foo which is:

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

var foo = getParameterByName('foo');

I now have a page where the HTML-Code looks like this:

<div class="bonus-button">
    <script language="javascript" type="text/javascript" src="https://t.tellja.eu/widget/button/PSIGn2?skin=194&t_mc=kwk&btn=rec_somecompany"></script>
</div>

What I want to do is replace t_mc=kwk with t_mc=value.of.foo. In this example it would mean

<div class="bonus-button">
    <script language="javascript" type="text/javascript" src="https://t.tellja.eu/widget/button/PSIGn2?skin=194&t_mc=test&btn=rec_somecompany"></script>
</div>

So far I have not found something I could really use or that works. Do you guys have any suggestions what I could use, e.g. replace didn't work.

Kesaja
  • 57
  • 6
  • 1
    Not sure changing the src of a ` – freedomn-m Mar 04 '19 at 10:14
  • A quick search gives this (there are probably others) for what appears to be your actual issue: https://stackoverflow.com/q/13748269/2181514 – freedomn-m Mar 04 '19 at 10:15
  • For the question as it stands, see https://stackoverflow.com/a/6021027/2181514 – freedomn-m Mar 04 '19 at 10:21
  • Possible duplicate of [How can I add or update a query string parameter?](https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter) – freedomn-m Mar 04 '19 at 10:21

1 Answers1

1

Use document.querySelector to get the script tag and using getAttribute get the src from the script. In the string obtained using split and join replace the value and set the src again using setAttribute

var a = document.querySelector('.bonus-button > script').getAttribute('src')
document.querySelector('div > script').setAttribute('src', a.split('t_mc=kwk').join('t_mc=value.of.foo'))
<div class="bonus-button">
  <script language="javascript" type="text/javascript" src="https://t.tellja.eu/widget/button/PSIGn2?skin=194&t_mc=kwk&btn=rec_somecompany"></script>
</div>
ellipsis
  • 11,498
  • 2
  • 13
  • 33
  • This seems to work in your example but I get Uncaught TypeError: Cannot read property 'split' of null. Why is that= Edit: I have it now thanks a lot and thanks for explaining what you did :) – Kesaja Mar 04 '19 at 10:32
  • That might be because the querySelector was for generally a div and script inside it. I have made edits and now it will work for the given div only provided there is no other div with the same class. Also make sure that there is a script tag and also has an src attribute. – ellipsis Mar 04 '19 at 10:37
  • yeah that was my mistake :( thanks again you saved my day :) – Kesaja Mar 04 '19 at 12:24