Forum Moderators: open

Message Too Old, No Replies

Hover color for link containing a javascript function

         

th1chsn

10:32 pm on Jul 11, 2003 (gmt 0)

10+ Year Member



Hello,

I am trying to have links change color when the mouse hovers over them. I am using CSS and it works fine except for the links that I am using javascript.

I use javascript links because I want some of the links to open in a new browser window of a certain size. Is there any way to add the onMouseOver and onMouseOut to the following link so that when I hover over it the font color changes?

<a href="javascript:;" onClick="MM_openBrWindow('http://www.domain.com','','scrollbars=yes,resizable=yes,width=545,height=300')">

Thanks in advance for the help.

Randy

Reflection

10:54 pm on Jul 11, 2003 (gmt 0)

10+ Year Member



Dont know why your css isnt working but if you want to use javascript...

onMouseover="this.style.color='#000'"
onMouseout="this.style.color='#FFF'"

tedster

11:15 pm on Jul 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your javascript function can be written two different ways, but your current code is half way between the two.

<a href="javascript:function()">
or
<a href="#" onClick="function()">

In fact, I often use a variant of the second one so users without javascript still get the new page.

<a href="newpage.html" onClick="function();return false;">

The return false; will keep the original page from changing for people who do have javascript working.

However, something in your HTML or your CSS is probably a bit off -- There's no reason the hover behavior shouldn't work on a js link. Is the anchor tag properly closed?

th1chsn

11:49 pm on Jul 11, 2003 (gmt 0)

10+ Year Member



That code was generated by Dreamweaver when I used the menu to establish the window size open. I also don't know why the hover behavior isn't working.

I am not that experienced with Javascript. Can you show me an example of the way you set it up? How did you declare that function before you called it in the html link?

tedster

1:42 am on Jul 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not that experienced with Javascript. Can you show me an example of the way you set it up?

The code I placed in that post essentially is the way I set it up, except that I used the expression function() instead of writing out the whole thing. I wanted to emphasize the differences between the two ways of coding and not have the visual distraction of all those parameters.

How did you declare that function before you called it in the html link?

I use an external .js file, but you could just as well define it in the <head> within a <script> element. Or, (wastefully, if you need it more than once) you can define the function right in the anchor element the way DreamWeaver did for you. Here is the way I would go about it, assuming that "url" is the address to load in the new window, and "winName" is the name of the new window object. You could also make any of the window parameters, including the size or position, into a variable. But I kept the following examples to just the basics.

IN THE HEAD SECTION OR EXTERNAL JS FILE
function popWin(url,winName) {
window.open(url,winName,'scrollbars=yes,resizable=yes,width=545,height=300')
}

IN THE HTML BODY
<a href="http://example.com/page.html" onClick="popWin('http://example.com/page.html','window1');return false;">Link Text</a>

th1chsn

2:30 am on Jul 12, 2003 (gmt 0)

10+ Year Member



Thanks! It works fine now. And your advice about the reason why the hover behavior wasn't working as a result of the anchor tags was correct also. I had a font tag mixed in there and that was the reason it wasn't showing the hover behavior.

Thanks again!