SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Knowledge is key 2 progression Tryst's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does href="javascript:" mean?

    Hi all,

    I have come across the following today...

    Code HTML4Strict:
    <a href="javascript:">image here that has a onclick javascript call</a>

    can anybody explain what it means, please?

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That alone does nothing, but normally javascript: precedes some JavaScript code to indicate that the browser should execute the code instead of treat the href attribute as a URL. It's not the recommended way of attaching JavaScript code to a link.

  3. #3
    Knowledge is key 2 progression Tryst's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Dan, and thanks for the reply.

    The full statement is below...

    Code HTML4Strict:
    <a href="javascript:">
    	<img src="images/panel_Plus.gif" onclick="changepic()" border="0"/>
    </a>

    What is the recommended way of attaching JS to a hyperlink?
    Last edited by Tryst; Apr 30, 2009 at 14:11. Reason: Posted too early.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The link is completely superfluous. Your image will already execute the code on click without the link being there.

  5. #5
    Knowledge is key 2 progression Tryst's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad,

    what I posted was coming from an XSL file, and I just noticed that the <a> contains some XSL...

    Code XML:
    <xsl:attribute name="onClick">javascript:document.images['<xsl:value-of select="$source-id"/>'].click()</xsl:attribute>

    which is basically assigning an onclick JS event to the <a> which is wrapped around this statement.

  6. #6
    Follow: @AlexDawsonUK silver trophybronze trophy AlexDawson's Avatar
    Join Date
    Feb 2009
    Location
    England, UK
    Posts
    8,111
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    It's not the recommended way of attaching JavaScript code to a link.
    The exception to the rule being Bookmarklets which of course are small JavaScript powered widgets which bring extended functionality to the page.


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
  •