so I have a code on my website that looks like this
<div class="bonus-button">
<script language="javascript" type="text/javascript" src="https://somedomain.de/widget/button/PSIG2ng?skin=134&t_mc=kwk&btn=rec_company"></script>
</div>
What I wanted to do is change the value of t_mc= to another one besides kwk depending on an url parameter. An example is www.somedomain.de/page?utm_source=email. When somebody enters the side using this parameter the code should look like this:
<div class="bonus-button">
<script language="javascript" type="text/javascript" src="https://somedomain.de/widget/button/PSIG2ng?skin=174&t_mc=email&btn=rec_company"></script>
</div>
Well it does that but still kwk is the value coming through and it doesn't stand in the source code, only in the elements tab in the chrome developer tool.
Somehow I believe I need to change it way earlier before everything is really there ... right?
if (window.location.search.indexOf('utm_source=') > -1) {
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 kwksource = getParameterByName('utm_source');
var scriptsrc = document.querySelector('.bonus-button script').getAttribute('src');
var scriptsrcbottom = document.querySelector('.bonus-button-bottom script').getAttribute('src');
document.querySelector('.bonus-button script').setAttribute('src', scriptsrc.split('t_mc=kwk').join('t_mc=' + kwksource));
document.querySelector('.bonus-button-bottom script').setAttribute('src', scriptsrcbottom.split('t_mc=kwk').join('t_mc=' + kwksource));
}
<div class="bonus-button">
<script language="javascript" type="text/javascript" src="https://t.tellja.eu/widget/button/PSIG2ng?skin=134&t_mc=kwk&btn=rec_company"></script>
</div>
<script>tag itself - something sent in the initial DOM response will get parsed no matter what (unless you callwindow.stop(), maybe)src. Instead, create a script tag using a script, and use what ever parameters you need to define the value forsrcattribute of that script. Then append the newly-created script tag to the body of the page.