/    Sign up×
Community /Pin to ProfileBookmark

Using javascript to modify every link on a page??

What I am trying to do is insert a simple piece of javascript somewhere on the page that will modify the href location of every link on the page to a specific value defined in the script

Such as

Lets say for example the page contains 3 links

<a href=”http://www.somesite.com/page1.html”>Some site</a>
<a href=”http://www.somesite.com/page2.html”>Some site 2</a>
<a href=”http://www.somesite.com/page3.html”>Some site 3</a>

I insert a piece of javascript code that changes all the links on the page to the following (without me manually having to change the links within the code)

<a href=”http://www.newsite.com/page.php?rp=http://www.somesite.com/page1.html”>Some site</a>
<a href=”http://www.newsite.com/page.php?rp=http://www.somesite.com/page2.html”>Some site 2</a>
<a href=”http://www.newsite.com/page.php?rp=http://www.somesite.com/page3.html”>Some site 3</a>

I am doing this so that once the bit of javascript is entered into the code of the page, all links on that page get replaced with a link pointing to a special offer page(including the original link as a variable)

Can anyone advise how I write the javascript to update all links on the page on the fly every time the page loads??

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@harrierdhMar 17.2010 — Here is one way.

var aArray = document.getElementsByTagName("A"); // get all the a tags

for (i = 0; i < aArray.length; i++) {

aTag = aArray[i];

aTag.href = "newHref" + i; //or whatever you want the new href tobe

}
Copy linkTweet thisAlerts:
@BWWebDesignsauthorMar 17.2010 — Thats great thanks

But one issue Im having with it

It not only replaces the href of the <a links but it also replaces the text of the link too

So <a href="/linkhere.html">Text here</a>

Gets replaced to <a href="http://www.somesite.com/page.php?rp=linkhere.html">http://www.somesite.com/page.php?rp=linkhere.html</a>
Copy linkTweet thisAlerts:
@BWWebDesignsauthorMar 17.2010 — Just to add this only seemed to happen on a few of the text links ones with mailto: in the link was what i noticed and an email address in the text
Copy linkTweet thisAlerts:
@harrierdhMar 17.2010 — var aArray = document.getElementsByTagName("A"); // get all the a tags

for (i = 0; i < aArray.length; i++) {

aTag = aArray[i];

aTag.href = "newHref" + i; //or whatever you want the new href tobe

aTag.innerHTML = "my new text";

}

Reply With Quote
Copy linkTweet thisAlerts:
@harrierdhMar 17.2010 — [CODE]
//Sorry here are better instructions.
//Place this between your <head> and </head> tags.

<script>
function changeLinks() {
var aArray = document.getElementsByTagName("A"); // get all the a tags
var aTag = "";
for (i = 0; i < aArray.length; i++) {
aTag = aArray[i];
aTag.href = "http://www.mysite.com/newHref" + i + ".html;
//or whatever you want the new href tobe
aTag.innerHTML = "my new text"; //changes the visible text
}
}
window.onload = changeLinks;
</script>
[/CODE]
Copy linkTweet thisAlerts:
@BWWebDesignsauthorMar 17.2010 — Thanks for all your help

With a few modifications it met my needs perfectly

[CODE]<script type="text/javascript">
var aArray = document.getElementsByTagName("a"); // get all the a tags
for (i = 0; i < aArray.length; i++) {
aTag = aArray[i];
aOldhref = aTag.href;
aOldText = aTag.innerHTML;
aTag.href = "http://www.somesite.com/page.php.php?rp=" + aOldhref; //Modify the tags
aTag.innerHTML = aOldText;
}
</script>[/CODE]


This now makes sure the original text of the link stays the text of the link and that the old href tag gets added to the end of the new href tag that is added in

Thanks for all the help
×

Success!

Help @BWWebDesigns spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...