#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    121
    Rep Power
    14

    calling javascript without event


    hello! is there anyway to call an javascript from an <a> tag without any event?

    Code:
    <a href="somepage.html" javascripthere>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    An href need only be a valid uri, which is not neccessarily a url!
    Code:
    <a href="javascript&#58; alert('Hello World!');">Say Hello</a>
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    121
    Rep Power
    14
    is there no way of calling it without having the user click on it?

    i know it doesn't make sense, because i could also simply do:
    Code:
    <script language="JavaScript"> 
    javascripthere; //e.g. alert('hello');
    </script>
    but please tell me if there's a way to do it.

    Can't I call code from that <a> tag without an event? Without the user clicking on it, without the user hovering over it? Just start it when the page loads? If not, is there any workaround to make that happen? To make code start inside an <a> tag no matter what?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    20
    Why do you need the code in an <a> tag if you can have it onload?
  8. #5
  9. PHPer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    La La Land
    Posts
    145
    Rep Power
    14
    For onload of page:
    <body onLoad="javascript:alert('hello world')">

    For clicking a link
    <a href="javascript:alert('Hello World')">Link</a>

    For rolling over a link
    <a href="#" onMouseOver="alert('Hello World')">Link</a>
    if (!$broke) {
    dontFixIt();
    }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    13
    You can also use javascript to invoke your a tag, assuming you give your a tag an id:

    Code:
    <a id="myLink" href="http://www.google.de/">Jump to Google</a>
    
    <script language="javascript">
      document.location = document.getElementById("myLink").getAttribute("href");
    </script>
    This also works with href="javascript:" links, but be careful if you want to use different targets, then they will be ignored.

    OR:

    Code:
    <script language="javascript">
      document.getElementById("myLink").click(); // don't know if mozilla supports this
    </script>

IMN logo majestic logo threadwatch logo seochat tools logo