Dear fellow coders, I am still learning the ropes of Javascript, so please bear with me.
I’ve encountered this snippet on a webpage:
<script type="text/javascript"> bb1 = "oldcode"; bb2 = "morecodehgere"; bb3 = 160000;</script>
My goal is to substitute 1% of all page loads from oldcode to newcode.
There are numerous occurrences of this code on the same page that need replacing.
window.onload = replaceScript;
function replaceScript() {
var randomNumber = Math.floor(Math.random()*101);
var toReplace = 'oldcode';
var replaceWith ='newcode';
if (randomNumber === 1) {
document.body.innerHTML = document.body.innerHTML.replace(/toReplace/g, replaceWith);
}
}
This is the current script I have implemented, but it’s not functioning as intended.
Is Javascript the best approach for achieving this task? If so, what would be the most efficient method?