Hi there, as I mentioned before my JavaScript skills are like zero since I only have experience in PHP and I wouldnt have a problem doing it in PHP, but I need it in JavaScript. On a Website are the following links: http://www.domain.com/all/ads/page-3/terms-keyword1 keyword2 keyword3/perpage-60/ Those links should be transformed into this (don't ask why ) http://www.domain.com/all/ads/page-3/terms-keyword1 keyword2 keyword3/perpage-60/&si=keyword1 keyword2 keyword3 Meaning, the script should search all links on the website which contain the string "term-" and then copy the the keywords that are between "term-" and the forward slash and but them at the very end of the link like &si=keyword1 keyword2 keyword3. Can anyone help me?
function changeLinks() { links = document.getElementsByTagName('a'); for (i=0; i<links.length; i++) { link = links[i].getAttribute('href'); pos = link.indexOf('terms-'); if (pos > -1) { pos = pos + 6; keywords = link.substr(pos, link.length - pos); pos = keywords.indexOf('/'); link = link + '&si=' + keywords.substr(0, pos); links[i].setAttribute('href',link); links[i].innerHTML = link; } } } Code (markup): It won't work on links with "term-", only those with "terms-". I'm still learning Javascript so maybe there is a better method.
Hey Cash, Wow, thanks a lot for all the work Unfortunately it doesn't seem to work. Might there be something wrong on the 2nd line because it starts with = document.getElementsByTagName('a'); and no variable is set for it?
Yeah, it should be: links = document.getElementsByTagName('a'); But DP sees links as a keyword and turns it into an ad link, even in code sections Is your browser setup to block those links?
I;m using chrome with AdBlock. Just deactivated it and now it I see the script properly. I changed all the words links to keylink in my script just to make sure, but for some reason it doesn't work. It doesn't give me an error message, but the links are not changing on my site. onload=function() { keylink = document.getElementsByTagName('a'); for (i=0; i<keylink.length; i++) { link = keylink[i].getAttribute('href'); pos = link.indexOf('terms-'); if (pos > -1) { pos = pos + 6; keywords = link.substr(pos, link.length - pos); pos = keywords.indexOf('/'); link = link + '&si=' + keywords.substr(0, pos); keylink[i].setAttribute('href',link); keylink[i].innerHTML = link; } } } Code (markup): Edit: Oh wait... now it worked real quick for some reason. I think I just have a conflict with another script. I'll see...
Okay it seems to be working. The only problem that I have now is that it seems to overwrite the linked word. Example: It should Display just "Page 1" that is linked to http://www.domain.com/all/ads/page-1/terms-keyword/perpage-20/&si=keyword But instead of displaying "Page 1" it displays the full link "http://www.domain.com/all/ads/page-1/terms-keyword/perpage-20/&si=keyword" on the page. Edit: I guess I just had to delete the last line of code. I'm testing it now...
Hey Cash, the code itself works perfectly fine. Thanks a million. I do have one problem though. It seems if an empty link is used in the source code of my website, your code doesn't work. <a name="top"></a> Code (markup): If that line is somewhere in the website, it breaks the script Any idea how to work around it? My guess it has to do with the line "link = links.getAttribute('href');" because it can't find the href attribute in that empty <a> tag. Edit: I managed to fix it, I implanted "if (links.hasAttribute('href')) " before the getAttribute line and it works like a charme. Again Thanks a million Cash
Oops! Yeah, I should have checked for empty links. If this was paid work, you'd be hitting Paypal for a refund about now. hasAttribute is a good way to check, but there may the occasional anchor with href="". Apparently, null and "" are both false so I would just use if (link) { ... } after the getAttribute line, but there may even be problems with that. You're lucky, I just clicked on that adlink out of curiosity. Man, I will never do that again!