Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems working with the <body> tag and events via Javascript and DOM.

    I'm pretty new to JavaScript, and as such I am trying to accomplish something I'm sure is relatively simple.

    I am trying to write a .js file to be included into an HTML document that will re-write the HTML document's <BODY> tag to include a number of events such as onKeyUp and onKeyDown.

    my .js file includes a function with the following:

    Code:
    var bodyEvents = document.getElementsByTagName('body').item(0);
    bodyEvents.setAttribute('onKeyDown', 'alert("boo")');
    Firstly, if I include the <script> tag in the head, then document.getElementsByTagName('body').item(0); "has no properties" and no attributes can be set.

    However if I include the <script> tag after the body tag, then document.getElementsByTagName('body').item(0); does have properties and I can set attributes (and it works in FF, but not IE)

    When I include the script in the head, I have tried calling this function with a window.onLoad hoping that that would cause the function to only fire once the body tag has been fully created in the DOM, but I still get the "no properties" error.

    So my questions are

    1. Is there a way to include the js within the head tag that can set attributes in the body tag to enable events to hook back into itself (such as onKeyDown)?
    2. If not, is it generally a bad idea to include the script tag within the <body> of the document?
    3. Setting these event attributes on the fly through js means they still work in FF, but IE no longer acknowledges these events unless they are hard coded into the HTML, is there a reason/way to solve this?

    Basically, my goal is to enable some simple functionality on a page with one simple js include, and not have to hardcode events into the <BODY> tag unless I *really* *really* *really* have to.

    Your knowledgeable answers are most appreciated

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try
    Code:
    window.onkeydown = function(event) { alert(event); }

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Use document.onkeydown instead of window.onkeydown.

    window.onkeydown works in FF but not in IE
    document.onkeydown works on both
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok thanks great That's solved that problem, I also used the following to handle events for both IE and FF

    the js file contains

    Code:
    document.onkeydown = show;
    
    function show(evt){
    	evt = (evt) ? evt : ((event) ? event : null);
    
        if ((evt.which && evt.which == 192) || (evt.keyCode && evt.keyCode == 192)){
    		dosomething();
    	}
    }
    Which works a treat (for FF and IE6)

    I do have another entirely different issue now, but I'll post that in a new thread. Once again thanks for all your help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •