SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript in html attributes

    I am trying to understand some existing script and came across the following application of javascript in html attributes in an <a> tag. It was generated by php so it might not be the most orthodox javascript.

    onmouseover="window.status='View this entry'; show(event, 'eventinfo-224-5'); return true;" onmouseout="window.status=''; hide('eventinfo-224-5'); return true;"

    I assume what is going on here is the same as the following javascript (show and hide being pre-defined functions):

    window.status='View this entry';
    show(event, 'eventinfo-224-5');
    return true;

    and:

    onmouseout=window.status='';
    hide('eventinfo-224-5');
    return true;

    Any comments on this way of including javascript scripts? Can one include scripts in any attribute in this manner? Is there no need for <script> tags? Other comments?

    Thanks,

    --Kenoli

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,494
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)

    inline javascript

    Inline javascript event handlers are supported, but are "old school".
    Similarly inline CSS styles are "old school".
    Something like
    HTML Code:
    <span style="color: red" onclick="window.close()">
    Close Window
    </span>
    may be OK, in that it "works", but this kind of code can get to be a real bear to maintain real fast.
    Current "best practice" is to have separate CSS and javascript files.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    --Kenoli

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "old school" can convey a sense of nostalgia. Using them for that reason is silly. "Deprecated" is a better word for it.

    It's best to avoid using inline event handlers whenever possible. I suggest you read up on Unobtrusive JavaScript and other JavaScript Best Practices.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •