Wrox Programmer Forums
|
BOOK: JavaScript 24-Hour Trainer
This is the forum to discuss the Wrox book JavaScript 24-Hour Trainer Jeremy McPeak; ISBN: 978-0-470-64783-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: JavaScript 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 29th, 2013, 02:12 PM
Authorized User
 
Join Date: Aug 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 18 Different results

Hi Jeremy.
I think you're saying, on p 174, that the handleonload function at the top of the page should not work if one calls onload = handleonload(); (that is, with the paren at the end).

Yet I'm getting the same result (the alert "Loaded" pops up) whether or not I include the parens with the assignment.

Code:
<!DOCTYPE html >
<html>
<script>
  function handleonload() {
    alert("Loaded");
  }
  onload= handleonload();
</script>
</html>
Am I misunderstanding what you're saying there?

Thanks.
 
Old February 5th, 2013, 10:30 AM
jmcpeak's Avatar
Wrox Author
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

Howdy, econophil.

It looks like the code works because you're executing handleonload(). By adding the () to the end of the function name, you execute that function. And since the page is a very simple page, it's difficult to notice the behavior as incorrect. Here's an example:

HTML Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
  <script>
  function handleOnload() {
    var fooDiv = document.getElementById("foo");
  
    alert(fooDiv === null);
  }
    
    onload = handleOnload();
  </script>
  
  
</head>
<body>
  <div id="foo">Hi!</div>
</body>
</html>
In this example, you'll see true in the alert box. The reason being because handleOnload() executes before the div element exists in the document, and getElementById() returns null if it cannot find the requested element. Now change:

Code:
onload = handleOnload();
to:

Code:
onload = handleOnload;
You'll see false in the alert box--meaning the getElementById() method found the div element.

I created a jsbin with this example, so you can play with it however you like: http://jsbin.com/elaqun/1/edit





Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch Help - Lesson 18 MinusZero BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 5 December 19th, 2012 08:47 PM
Lesson 18 Exercise 3 SamW BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 6 December 11th, 2012 12:55 AM
Lesson 18 - TryIt sendalot BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 June 6th, 2012 10:30 AM
Lesson 6 Try It zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 2 June 6th, 2011 10:07 PM
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.