homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

How do I code hyperlinks in Javascript?

 8:42 pm on Jul 6, 2002 (gmt 0)

I have 10 sites (domains) that I want to link to each other For the benefit of the surfer, NOT for the search engines. Am I right in thinking that I can interlink these sites in JavaScript and avoid the google penalties that may otherwise be imposed?

If so, what should the code look like?



[edited by: tedster at 9:10 pm (utc) on July 6, 2002]
[edit reason] changed java to javascript - very different things! [/edit]


Torben Lundsgaard

 8:51 pm on Jul 6, 2002 (gmt 0)

This should do it:

document.write('<A HREF="http://www.mysite.com">My site</A>')

However, if you have got unique content on all 10 sites I recommend that linke use normal SE friendly links.


 9:22 pm on Jul 6, 2002 (gmt 0)

Yep, that's one way to do it - but you need to escape the backslashes within the document.write, or you'll confuse the poor browser.

Also, it's best to use the type attribute - the language attribute is going to be leaving us. You can use both, to keep old browsers happy:

<script language="JavaScript" type="text/javascript">
document.write('<a href="http:\/\/www.mysite.com">My site<\/a>')


 10:21 pm on Jul 6, 2002 (gmt 0)

Thanks Guys,
Yes I do have unique content on the sites, but I think because they are fairly new and don't have very many inbound links yet, that they got penalised in the latest google update. My PR dropped by 1 point on each site which I can only think was due to it looking like a closed loop from what I've read here.... so I just thought I should play safe..



 9:55 am on Jul 8, 2002 (gmt 0)

JavaScript also has a function called String.link() which adds <a> tags:

document.write('My site'.link('http://www.mysite.com'));

That seems like a long way around the problem, but if you have stored the links and texts in variables, it makes it easier to automate the process. So, instead of, for example:

document.write('<a href="'+hyperLink[i].url+'">'+hyperLink[i].text+'</a>');

you can write:


You could probably make the code even easier with the with statement, though this is discouraged:

with(hyperLink[i]) document.write(text.link(url));

The obvious drawback is that you can't add any extra attributes, such as title or class.

(Yeah, I'm always on the lookout for ways to cut down on the amount of code I write.)


 11:01 am on Jul 8, 2002 (gmt 0)

I think your description could be really useful to me....
My problem is that whilst I can grasp the benefits , I'm finding it hard to visualise how I could store the links and texts in variables, and where....if that makes sense?
And then what code would I put where for the example?
[mydomain1.com...] first
[mydomain2.com...] second
[mydomain3.com...] third

I'm sorry to be so green....

I would love to cut the code down on my pages... One thing I would love to be able to do is to put one line of code in a page or css file that would have the same effect as putting

onMouseOver='window.status=" ";return true' onMouseOut='window.status="Done";return true'

on all my affiliate links! I can't find a way of doing it though which is a real shame. I guess I just need something that would disable the status bar for all mouseover actions....

Hey ho...



 11:10 am on Jul 8, 2002 (gmt 0)

but you need to escape the backslashes* within the document.write

I'm curious - why do you need to do this? *aren't they forward slashes?


 11:16 am on Jul 8, 2002 (gmt 0)

> I'm curious - why do you need to do this? *aren't they forward slashes?

I think that's what he meant. A browser reading JavaScript sometimes will see the / as a termination, so the \ is added to make \/ which fixes it.


 11:23 am on Jul 8, 2002 (gmt 0)

I figured that but I get mixed up myself between the back/forward bit.*

OK, but that's only the case when using regular expressions. Are there browsers that will have problems with this even when using normal strings "" or ''?

*I just found a top-tip that'll help me remember this which I'd never thought of before: A forward slash is leaning forward and a back slash is leaning backwards (if you read left-right anyway;)).


 9:19 pm on Jul 8, 2002 (gmt 0)

I don't think disabling the status line is a good idea -- why would you want to do it? If you really need to, try this (untested, I don't know if it works):

for(var i=0; i<document.links.length; i++){
document.links[i].onmouseover='status=""; return true;';

You'd need to call it after the document has loaded -- I think.

(Don't use status="Done" -- it's not helpful. Besides, I have a German OS, so mine doesn't say "Done" when a page has finished loading, it says "Fertig".)

For storing links in variables -- well, it probably depends on stuff like how many times on one page the links are to appear. Twice, and it might be worth it. Once, and... you'll have to weigh up the pros and cons.

Here's one suggestion (again, untested):

var linkArr='myfirstsite.com=My first site*mysecondsite.com=My second site*mythirdsite.com=My third site'.split('*');
var hyperLink=new Array();
for(var i=0; i<linkArr.length; i++){
hyperLink[i]=new Object;
var linkPair=linkArr[i].split('=');

You should end up with an array called hyperLink, each element being an object with two properties: url is the URL of the link (its destination), and text is the text for the link. The first line contains the link URLs and the link texts -- each pair is separated by an asterisk, within each pair the URL and text is separated by an = sign. You can use whatever symbols you want, as long as you remember to edit the relevant split() statements.

Note that I have cut corners by adding "http://www." to the beginning of each URL within the loop. That will only work, of course, if all of your URLs begin with [www....] -- which they may not.


 9:59 pm on Jul 8, 2002 (gmt 0)

Guten Abend :)

but I found I can do this <BODY onmouseover="window.status=' ';return true;">
and it cuts out all my excess code with one line.
I do want to disable the status bar, as it enables me to hide affiliate codes. Surfers are getting quite savvy and some will use the status bar to see the link and then type in just the domain minus the all important affiliate ID.

Of course if they really want they can still view source, but hey if they're going to go that far to avoid clicking a link, good luck to them.

Tchuss (spelling) Ich spreche nur ein bissien Dutche (Englander)


 7:24 am on Jul 9, 2002 (gmt 0)

How many people actually do that? Anyway, you don't need to View Source. You can disable JavaScript; or you can right-click on the link, copy the URL to the clipboard, paste it into the address bar and edit it at will; or right-click and view Properties... the possibilities are endless.


 2:15 pm on Jul 9, 2002 (gmt 0)

I just tried the disable status bar code and it really did that BUT...in IE6+ the entire url shows up just fine in the address bar on which all I need to do is delete part of the address and go from there????????

Why bother???



 2:32 pm on Jul 9, 2002 (gmt 0)

Sure ther are hundreds of ways round it, depending on how your affiliate codes work. Mine work with cookies, so once they click the link the cookie is there (unless they delete cookies)no matter what they then do with the URL.

I don't say that this is fool proof, it just works for me, I have tested with and without doing this and I get an increase of about 5% in sales when I use it. Its each to his own of course.



 4:32 pm on Jul 9, 2002 (gmt 0)

> *aren't they forward slashes?

Yes, I goofed there. Left, right, forward, back - I often mis-write these things. Some kind of mewntal defect, I'm sure.


 5:17 pm on Jul 9, 2002 (gmt 0)

But the code example you gave is correct? Right?

I hope so.... I uploaded it already...


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved