SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    document.body.onclick - Why Would This Not Work ??

    Now here's something that I thought was basic and simple to do...

    Code JavaScript:
    <script type="text/javascript">
    	document.body.onclick = alert('test');
    </script>

    ...I put that just before my close body tag, thinking that from now an anytime I clicked on my page I'd see an alert saying "test".

    What actually happens, though, is when the page loads the alert pops up and nothing happens afterward.

    The clicks are just ignored.

    Also there are no javascript errors coming up so I don't know what's the cause of the problem.

    Why wouldn't this work ??




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    Non-Member
    Join Date
    Aug 2006
    Location
    Kansas City
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try:


    <script language="javascript">

    window.onClick= function()
    {
    alert('test');
    }

    </script>

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by KCChiefs View Post
    try:


    <script language="javascript">

    window.onClick= function()
    {
    alert('test');
    }

    </script>
    You misspelt both type="text/javascript" (there is no language attribute any more) and onclick which must be spelt with a small C as JavaScript is case sensitive.

    Try:

    Code:
    <script type="text/javascript">
    window.onclick = function() {alert('test');}
    </script>
    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="^$">

  4. #4
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Okay.

    Thanks that worked.

    I figured the alert(); being a function itself wouldn't need to be in the function(){} syntax but I guess not.




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse


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
  •