SitePoint Sponsor

User Tag List

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

    Simply JavaScript

    First, I would like to complement the authors for a wonderfully presented introduction to JavaScript. Their presentation and style, not to mention the humour, suites a beginner like me.

    However, the snippets, although they make perfect sence in their own right, would be better served if the authors included them in the bigger picture, rather than having me go all around the Internet trying to find out where to place these in the document.

    As an example, Page 72:

    /*
    var listItems = document.getElementsByTagName("li");
    for (var i = 0; i < listItems.length; i++)
    {
    alert(listItems[i].nodeName);
    }
    */

    does not work when placed in an external .js file in the <head> section, but will work when place "after" the last </ul> item in <title>Tag Name Locator</title> document that it references, on page 70.

    This may seem trivial to a JavaScript expert, but people like myself buy these books because we don't know how to do these things and if I may say so, should be treated even dumber than a computer!

    Many thanks.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    learning

    Hi someTimesVarable, welcome to the forums,
    Yes, I agree that learning can be a bit frustrating at times. Once someone knows so much, a lot of stuff seems obvious and mentionng it to "newbies" gets over-looked.
    AFAIK, with most (all?) browsers, script in a page's head (that isn't in a function) runs when the head loads, before the rest of the page loads in. Ideally, code that needs the page to have been loaded will be in a function that gets called after the page finishes loading (or you can put the script at the bottom of the page's body, as you have learned ).
    For example, if you put the snippet in a function in an external script

    snippet.js
    Code:
    function list_item_alerts()
    {
    	var listItems = document.getElementsByTagName("li");
    	for (var i = 0; i < listItems.length; i++)
    	{
    		alert(listItems[i].nodeName);
    	}
    }
    you could then do
    HTML Code:
    <script type="text/javascript" src="snippet.js"></script>
    <script type="text/javascript">
    document.onload = list_item_alerts();
    </script>
    I personally like all my script in external files called from the head, but I have read where some like larger/slower scripts in the end of the body instead. this is because script in the head can delay page loading. When it's in the end of the body, other page content (eg. text, images) can start to get rendered while the script finishes loading in.

    In any case, I've found that the lessons I've learned the hard way "stick" better with me. I bet you won't forget this particular lesson too soon either.
    Last edited by Mittineague; Mar 4, 2008 at 18:56.

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anything wrong with using the Core library (as provided by the authors) to solve this problem? I agree with Mittineague that there could be just a bit more hand holding when it comes to implementing such things. It took me a while to figure out how to specifically use the Core library: where to place the library, how to include it, etc.

    I also agree that Simply Javascript is a very nice book. I have already taken a couple of JavaScript courses, but this book makes the key concepts very accessible, and I appreciate the author's insights regarding best practices.


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
  •