Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Correct xhtml strict tag for a link.

    Hi,

    When I have a sentence in a paragraph like

    <p>HAPPY 2004 TO ALL OFF YOU</p>

    and I like to add an onclick event to the word 2004 as well as a hover event with css, which is the correct tag to use?

    <a>
    <span>
    <else>

    Thanks for Your time,
    Jerome

  • #2
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It depends on:
    • If you want IE compliancy, use <a>, as it's the only tag IE supports :hover on.
    • What are you going to do with the onclick event? That'll really tell which tag you should use.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts
    IE only supports hover on <a> ? hmm !

    I want to get rid off a declaration like:

    <a href="javascript:function();">2004</a>

    xhtml strict, for a commercial (thus IE supporting) site

    Jerome

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    This would be the way to do it:

    <a href="Alternate.htm" onclick="javascript:function();" title="The title of the link">2004</a>

    Unless I am forgetting something...
    OracleGuy

  • #5
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by oracleguy
    This would be the way to do it:

    <a href="Alternate.htm" onclick="javascript:function();" title="The title of the link">2004</a>

    Unless I am forgetting something...
    Yep. onclick="function()", no protocol needed there. Also, if you don't want to change the page, href="#".
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're right about the protocol not being necessary for onclick (although it doesn't matter if it is there), but you're wrong about the use of "#" for the href - you should never do that - always make the href go somewhere and then return false on the onclick handler, otherwise you'll create a link that for non-javascript browsers does nothing at all.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #7
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Originally posted by brothercake
    You're right about the protocol not being necessary for onclick (although it doesn't matter if it is there), but you're wrong about the use of "#" for the href - you should never do that - always make the href go somewhere and then return false on the onclick handler, otherwise you'll create a link that for non-javascript browsers does nothing at all.
    Well, onclick="javascript:bla()" actually isn't specifying any protocol. I'm probably going to end up biting my tongue since I haven't tested this, but I believe what is going on is that you are labeling the codeblock "bla()" with the label "javascript". Check the specs for labelled statements.
    Last edited by jkd; 01-02-2004 at 11:24 PM.

  • #8
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by brothercake
    You're right about the protocol not being necessary for onclick (although it doesn't matter if it is there), but you're wrong about the use of "#" for the href - you should never do that - always make the href go somewhere and then return false on the onclick handler, otherwise you'll create a link that for non-javascript browsers does nothing at all.
    Good point. Maybe you could do some detection to be really friendly:
    Code:
    <script type="text/javascript">
    //<![CDATA[
      document.getElementById('message').getElementsByTagName('a')[0].addAttribute('onclick', 'function() {alert(\\'hello world\\')}');
    
    //]]>
    </script>
    <h2 id="message">HAPPY <a>2004</a> TO ALL OF YOU!</h2>
    Last edited by me'; 01-03-2004 at 11:56 AM.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by me'
    //<![CDATA[

    //]]>
    What is it with this putting script in commented CDATA braces? If it's an HTML document surely it shouldn't have CDATA sections, and if it's an XML document then surely the commenting-out is not necessary?
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

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