/    Sign up×
Community /Pin to ProfileBookmark

Changing Link URL with JavaScript

Hello. I am dynamically generating some pages that have link URLS in the format of “/folder/page.html” (no domain name listed). I want the links to include the proper domain, not MY domain, and was hoping a JavaScript could take care of this “on the fly.”

I set up an external .js file with the following code:


——–

for (var i=0; i<document.links.length; i++) {
if (document.links[i].href.indexOf(‘/’) > -1) {
document.links[i].href = “http://www.realdomain.com/”+document.links[i].href+””;
}
}

——–

I thought this would work, but the end result looks like this: http://www.realdomain.com/http://www.mydomain.com/folder/page.html

Can anyone tell me how to change the .js file to get rid of that wrong assumed domain name that is automatically added to the page so that I can link to these pages properly?

Thanks for your time.

Sincerely,
Jared

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayDec 08.2003 — Since it's a consistant 11 characters:

[code=php]
var tempStr;
for (var i=0; i<document.links.length; i++) {
if (document.links[i].href.indexOf('/') > -1) {
tempStr=document.links[i].href.substring(11,document.links[i].href.length);
document.links[i].href = "http://www.realdomain.com/"+tempStr+"";
}
} [/code]
Copy linkTweet thisAlerts:
@fredmvDec 08.2003 — &lt;script type="text/javascript"&gt;
//&lt;![CDATA[
onload = function()
{
var a = document.links;
for(var i=0; i&lt;a.length; i++)if(/^http/.test(a[i].href))a[i].href = 'http://www.realdomain.com/'+a[i].href.substring(a[i].href.lastIndexOf('/')+1, a[i].href.length);
}
//]]&gt;
&lt;/script&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.mydomain.com/file1.html" onclick="alert(this.href);return false;"&gt;<a href="http://www.mydomain.com/file1.html">http://www.mydomain.com/file1.html</a>&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.mydomain.com/file2.html" onclick="alert(this.href);return false;"&gt;<a href="http://www.mydomain.com/file2.html">http://www.mydomain.com/file2.html</a>&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.mydomain.com/file3.html" onclick="alert(this.href);return false;"&gt;<a href="http://www.mydomain.com/file3.html">http://www.mydomain.com/file3.html</a>&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
×

Success!

Help @jaredwalczak 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.12,
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,
)...