Help - Search - Members - Calendar
Full Version: Change link color
HTMLHelp Forums > Programming > Client-side Scripting
Emu
Due to a long story which we don't need to go into here I am trying to change the color of a link after it has been clicked on WITHOUT using CSS.

When the link is clicked some javascript is called. I can do this in IE by having adding this "this.style.color = 'black'". However this doesn't work in other browsers. Anyone know how I could get this to work in other browsers? I've spend hours on this...

Thanks for yuor time!

(btw when I was just signing into this forum I had touble when using firefox!)
pandy
I assume the link doesn't lead anywhere? That it's used just to hang the onclick on? Try if what you have now works if you don't use a link at all. Use a P or SPAN or something else and style it accordingly.
Emu
Thanks I seem to have fixed the issue...I don't understand why but if I have a onCLick in the anchor tag it works across all browsers. Thanks for your reply anyway.
pandy
I think it's because 'color' is for text color and the browser's default link styling overrides it. You can't easily do 'a:link', 'a:visited' and so on with JS, the best would be to let JS assign a class name and have a rule for that class in your style sheet. I could be wrong here though. I don't really remember and it's a lazy Saturday. tongue.gif
pandy
Wait, what are you saying now?
CODE
I don't understand why but if I have a onCLick in the anchor tag it works across all browsers


So now it works with A? What did you use before if you didn't use inclick?
Emu
The actual target of the link is JS anyway. I had just added the code in there. (in the anchor href) maybe the self doesn't resolve properely in non IE browsers that way? Well it doesn't matter it all works now. I don't have much experience with JS maybe I should have known better...

QUOTE(pandy @ Aug 22 2009, 09:47 AM) *

Wait, what are you saying now?
CODE
I don't understand why but if I have a onCLick in the anchor tag it works across all browsers


So now it works with A? What did you use before if you didn't use inclick?
Christian J
QUOTE(Emu @ Aug 22 2009, 03:15 PM) *

(btw when I was just signing into this forum I had touble when using firefox!)

What kind of trouble?
Emu
QUOTE(Christian J @ Aug 22 2009, 10:41 AM) *

QUOTE(Emu @ Aug 22 2009, 03:15 PM) *

(btw when I was just signing into this forum I had touble when using firefox!)

What kind of trouble?


Well it didn't log me in...or give me an error. I had to change to IE.
pandy
You mean like <a href="javascript:...">? Never ever use that. It's very bad. ninja.gif
Emu
Yes exactly like that. I didn't know it was bad. In fact I am using technology that has this built into it and it is often used in examples... Is it really bad? How so? I know little about JS.

QUOTE(pandy @ Aug 22 2009, 11:12 AM) *

You mean like <a href="java script:...">? Never ever use that. It's very bad. ninja.gif

Darin McGrew
You should use href for a real URL, not for a JavaScript pseudo-URL. Put the JavaScript in the onclick event hander, with "return false" if the JavaScript replaces the real URL. That way, those with JavaScript enabled get the JavaScript, and those with JavaScript disabled/unavailable get the real URL.

There's an example in the FAQ:
http://htmlhelp.com/faq/html/links.html#window-specify
pandy
It isn't bad in the sense it doesn't work (JS-wise). It's more of a bad practice, for the reason Darin mentioned and others. It's an ancient way to attach JS to a link, from before there were any event handlers. It should be put to rest. And yes, online examples can be bad. One tutorial site copies the other and so they have been doing since 1995 or whenever... so those old baddies tend to stick.
Christian J
There might be cases when a link is meant only for javascripting, similar to a form button, and doesn't have/need a URL (though I can't come up with any examples at the moment that can't be more or less recreated with server-side scripting). In such cases

CODE
<a href="java script:foo();">foo</a>


might be used, even though it's better to create the whole link from javascript so the page would make sense for users without JS. You could also style some other element to look like a link, but then users can't access it with keyboard navigation.
pandy
QUOTE(Christian J @ Aug 23 2009, 01:16 AM) *

There might be cases when a link is meant only for javascripting, similar to a form button, and doesn't have/need a URL (though I can't come up with any examples at the moment that can't be more or less recreated with server-side scripting). In such cases

CODE
<a href="java script:foo();">foo</a>


might be used


Why? What's the benefit of that? I'd say it would be better not to use a link at all if it isn't going to act as link.
Emu
Well when I say there are examples. I am using oracle apex - which probably means nothing to most people. this comes with these JS links built into the framework and the examples from oracle also do this.

Perhaps because oracle apex works with only in a ajax/JS enabled browser? And yes as Christian guessed the link is only meant only for JS....possibly a button could have been used instead of a link but it would not have been visually pretyy.

I actually did look here on WDG at the anchor tag and it didn't explicitly list a onClick action so I didn't even try this myself. Just checking again it did say also there were "core" attributes which include the onClick. I guess I have just learnt something.... Thanks guys!

(BTW I am new to oracle apex but it actually does seem like a very neat piece of technology don't let me put you off it too much!)
Christian J
QUOTE(pandy @ Aug 23 2009, 04:23 AM) *

Why? What's the benefit of that? I'd say it would be better not to use a link at all if it isn't going to act as link.

Compared with a form button the user is able to select and copy link text. Also long link texts can wrap in narrow windows.

Regarding appearance you might style make a form button to resemble a link, but it seems easier to make a link in the first place. blush.gif

Both form buttons (except submit buttons) and pseudo-links will be useless if JS is not used, so in that respect both are equally bad.
pandy
I meant one can as well use a P, a SPAN or whatever fits the situation. A link isn't needed.
pandy
QUOTE(Emu @ Aug 23 2009, 07:34 AM) *

I actually did look here on WDG at the anchor tag and it didn't explicitly list a onClick action so I didn't even try this myself. Just checking again it did say also there were "core" attributes which include the onClick. I guess I have just learnt something.... Thanks guys!


Sneaky, isn't it? biggrin.gif

This page at the W3C is also useful.
http://www.w3.org/TR/html401/index/attributes.html
Christian J
QUOTE(pandy @ Aug 23 2009, 10:27 PM) *

I meant one can as well use a P, a SPAN or whatever fits the situation. A link isn't needed.

But then you can't use keyboard navigation.

In some browsers (IE and Opera) you can use keyboard navigation to reach an A elements without HREF attribute if you give it a TABINDEX instead:

CODE
<a tabindex="1">foo</a>

but to make keyboard navigation work in e.g. Firefox I guess a real link or form buttton is needed.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.