/    Sign up×
Community /Pin to ProfileBookmark

I need to place a link before all links on a page

Hi,
I need to place a URL in front of all the URL’s on one of my pages. I have a page that dynamically recieves articles. These articles have links that take the user to the full story.
Example:
[url]http://www.url_to_full_story.com[/url]
should be:
my_special_urlhttp://www.url_to_full_story.com

So “my_special_url” must be pasted in front of all existing links on the page.
Are there anyone who could make this script for me? With comments would be great, so I can see what each part of the code does.

Kind regards
<morten />

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@neilemrichOct 10.2006 — Hi,

easiest way is to make the link like so:

<a href="javascript:void(0)" onClick="document.location.href = 'my_special_url'+URL;">

It does depend what form your URL is in, i.e. if it's in a javascript variable then you can reference it directly or if you are using php or asp then you will have to print it out into the javascript as another string.

Hope this helps. ?
Copy linkTweet thisAlerts:
@_lt_morten___gt_authorOct 10.2006 — Thanks for replying.

I can't touch the links themselves for various reasons. I have another JS in the document that tells all links to open up in a new window. What I need is something similar which takes all links and just attaches the url "my_special_url" in front of them. To see what I had in mind have a look at the "new window"-script:

[CODE]<script type="text/javascript">
function tagsBlankify()
{

var as = document.getElementsByTagName('a')
var alen = as.length

for (var i=0; i < alen; i++)
{

var link = as[i]

if (link.getAttribute('href') && link.getAttribute('rel') != 'keepasis')
{
link.target = '_blank'
link.title= 'Opens in new window'
}
else
{
link.title = 'Kept as is'
}
}
}


onload = function()
{
tagsBlankify()
}

</script>[/CODE]


Any chance you could help with something like that?

Kind regards

<morten />
Copy linkTweet thisAlerts:
@felgallOct 10.2006 — Is this the change you want?

&lt;script type="text/javascript"&gt;
function tagsBlankify()
{

<i> </i>var as = document.getElementsByTagName('a')
<i> </i>var alen = as.length

<i> </i>for (var i=0; i &lt; alen; i++)
<i> </i>{

<i> </i> var link = as[i]
<i> </i> [b]if (link.getAttribute('href'))
<i> </i> link.href="my_special_url"+link.href;[/b]
<i> </i> if (link.getAttribute('href') &amp;&amp; link.getAttribute('rel') != 'keepasis')
<i> </i> {
<i> </i> link.target = '_blank'
<i> </i> link.title= 'Opens in new window'
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> link.title = 'Kept as is'
<i> </i> }
<i> </i>}
}


onload = function()
{
tagsBlankify()
}

&lt;/script&gt;
Copy linkTweet thisAlerts:
@nitro2k01Oct 10.2006 — Try this:window.onload=function(){
var as=document.getElementsByTagName("A");

<i> </i>for (var i=0; i&lt;as.length; i++){
<i> </i> as[i].href="my_special_url" + as[i].href;
<i> </i>}
}
Copy linkTweet thisAlerts:
@_lt_morten___gt_authorOct 10.2006 — Thank you both for replying.

I think I'll go with felgalls script as it seems to incorporate both the "new window" and "attach url" functions I need.

Thanks for your swift help, guys!

kind regards

<morten />
×

Success!

Help @_lt_morten___gt_ 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...