SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Processing HTML events in Javascript - problem

    In the following code I am trying to print out the value of document.body.onclick from Javascript:

    <BODY onclick="alert('ok')">

    ...

    <script type="text/javascript">
    alert(document.body.onclick);
    </script>

    ...
    </BODY>

    However, the alert in Javascript code prints out "undefined" when I open this page and not the actual value of onclick. Could somebody help me?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The JavaScript in the body tag is actually attached to the window and not the body. It is only placed there because the HTML doesn't have a window tag to attach it to.

    alert(window.onclick);

    You would do better to move the JavaScript out of the body tag so as to avoid confusion such as this.

    HTML is static and has no events at all.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •