SitePoint Sponsor

User Tag List

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

    Question Inline JS works, but not included JS

    Hello...

    I only have moderate Javascript experience, but I wanted to try making my own non-disruptive, easy-to-implement navigational expand/collapse menus. I came up with the following (source).

    As far as I can tell it works like a charm. So, patting myself on the back, I set out to divide it into seperate HTML, CSS, and JS files like us modern web designers are supposed to. But changing the <body onload="start();"> into document.onload=start(); broke it. The start() function still runs, but now the document.getElement* functions return empty results.

    Why? Am I somehow changing what "document" refers to? (I didn't think so; in my testing it's still returning the correct document.title.) I couldn't find any reference to that behavior anywhere. Any thoughts on what I'm doing wrong here?

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using window.onload.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the event handler is inline in the html, you need to include the parentheses after the funtion name:

    <body onload="start();">

    When you are assigning a function to a property of an object(e.g. document), then you don't use the parentheses:

    document.onload = start;

  4. #4
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha! Neither answer worked by itself, but when I did those both together, bam. Thank you both!
    Last edited by capnchunky; Mar 8, 2005 at 09:01. Reason: 'cuz I spoke too soon


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
  •