I am new to coding and so to JavaScript as well.
I tried to find a solution to my problem on Stack Overflow, but only found answers to parts of it. I patched up a code below that obviously doesn't work.
I aim to select links on a web page with particular domain in the href attribute, then change the href of the selected links so that only the first 106 characters of the url string would be kept and a new bit ("groupid=provider") added to it. These links are also supposed to receive a class attribute with a value of "wrongLink". At the moment when I load the web page that has the script below, every link on the webpage is affected, regardless of the domain in the url of the link.
Code:
window.onload = function() {
/* onload code */
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
var urltobechanged = anchors[i].href;
/* urltobechanged to store the url */
var domaincheck = urltobechanged.startsWith("example.com");
if (domaincheck = true){
/* if the stored url starts with "example.com", then.. */
anchors[i].href = urltobechanged.substring(0,105) + "groupid=provider";
/* keep the first 106 characters of the url and add "goupid=provider" to it */
anchors[i].setAttribute("class", "wrongLink");
/* add class with "wrongLink" attribute to the the changed link element */
}
}
}
Any help would be much appreciated. Thanks