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.

4
  • 1
    Not sure changing the src of a <script> tag that has already run would do what you think it would do. Looks like an XY problem where you want to load a different script. Changing the url of eg an <img> tag would be trivial with your existing code. Commented Mar 4, 2019 at 10:14
  • A quick search gives this (there are probably others) for what appears to be your actual issue: stackoverflow.com/q/13748269/2181514 Commented Mar 4, 2019 at 10:15
  • For the question as it stands, see stackoverflow.com/a/6021027/2181514 Commented Mar 4, 2019 at 10:21
  • Possible duplicate of How can I add or update a query string parameter? Commented Mar 4, 2019 at 10:21

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

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 :)
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.
yeah that was my mistake :( thanks again you saved my day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.