SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Anchor problem

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Anchor problem

    Hello. I try to change href attribute value of anchor link for directing another url rather than the one specified in html document. Below code didnt work. What am I doing wrong?
    Thanks in advance.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>


    <script type="text/javascript">

    function redirecting(){
    document.first.href="http://www.google.com";
    }

    </script>


    </head>
    <body>

    <a href="http://www.yahoo.com" name="first" onClick="redirecting();" >Yahoo</a>


    </body>
    </html>

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure that will work the way you want but

    Code:
    function redirecting(what) {
    what.href = 'http://www.google.com';
    }
    <a href="http://www.yahoo.com" name="first" onclick="redirecting(this);">Yahoo</a>


  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. The code solved the problem. But why below code didnt worked. It seems to be a valid ;

    document.first.href="http://www.google.com";

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I couldn't tell you to be honest. I don't like that way of coding, I prefer to access everything directy via id's or like above, i've passed the link to the function.



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
  •