Say you had another method on the page like this:

function somethingElseOnClick() {

identifier = "cheese";

}

When this function is called, your identifier variable has been overwritten,
so when your hover method comes to access it, it's not the right value any
more.

With regards to the unobtrusive javascript comments: yes, it's best practice
to use a library like jQuery or Prototype, but for simple one-use things, or
starting to learn JavaScript, its not a bad idea to write it out by hand.

Steve
2008/12/9 Brett Patterson <[EMAIL PROTECTED]>

> Hi Steve,
>
> How could another method change the element the identifier variable is
> pointing at? I thought that that could only occur if I changed the id
> attribute or the variable itself, or the argument (here
> namedFunction('timer')) where timer is the argument?
>
> Hi Chris,
>
> Is not acceptable to put event handlers like onhover and onclick in an HTML
> page? Sorry, but I am still learning JavaScript.
>
> On Tue, Dec 9, 2008 at 10:45 AM, Chris Taylor <[EMAIL PROTECTED]>wrote:
>
>> Hi Brett,
>>
>> The problem isn't this:
>>
>> > var whatever = document.getElementById(layer);
>>
>> I's this:
>>
>> > onhover="namedFunction('timer')
>>
>> What you're doing is mixing JavaScript in the HTML of the page. What you
>> should do is use a "listener" on your link to see when it is hovered over.
>> This code uses the prototype library [1] but you can do the same thing with
>> other libraries such as jQuery and mootools. I recommend you take a look at
>> one of those libraries to help you with this stuff. (Warning: I've not
>> tested this code!)
>>
>> In the <head> of your page:
>>
>> <script type="text/javascript">
>>
>> // listen for the page being loaded completely
>> Event.observe(window, 'load', HoverListener, false);
>>
>> function HoverListener()
>> {
>>        // for each link with the class 'hover'
>>        $$('a.hover').each(function(element)
>>        {
>>                // listen for the 'mouseover' event on this link and
>> execute the 'RunCode' function when it happens
>>                Event.observe(element, 'mouseover', RunCode, false);
>>                // also listen for the 'focus' event on this link for
>> keyboard-compatibility
>>                Event.observe(element, 'focus', RunCode, false);
>>        });
>> }
>>
>> function RunCode(e)
>> {
>>        // get the element which triggered the event
>>        var el = Event.findElement(e, 'A');
>>
>>        // now do your code! In this example I'm just alert()ing the text
>> in the link
>>        alert(el.innerHTML);
>> }
>>
>> </script>
>>
>> You can apply the class "hover" to any link, it will execute the RunCode
>> function above when that link is hovered over (or receives focus from a
>> keyboard action):
>>
>> <a href="somepage.html" class="hover">Hover over this link...</a>
>>
>> Hope this helps,
>>
>> Chris
>>
>> [1] http://prototypejs.org
>>
>>
>> This message has been scanned for malware by SurfControl plc.
>> www.surfcontrol.com
>>
>>
>> *******************************************************************
>> List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
>> Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
>> Help: [EMAIL PROTECTED]
>> *******************************************************************
>>
>>
>
>
> --
> Brett P.
>
> *******************************************************************
> List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
> Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
> Help: [EMAIL PROTECTED]
> *******************************************************************
>


*******************************************************************
List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
Help: [EMAIL PROTECTED]
*******************************************************************

Reply via email to