Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 28th, 2005, 03:48 PM
Registered User
 
Join Date: Dec 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error: Need to update code in book

The code for the event handler, which makes IE compatible with the DOM handler, needs to be updated to work properly.

Specifically, the document.body.scrollLeft and document.body.scrollTop values always return zero (0), because they include the 'px' at the end.

Therefore, the code needs to have parseInt() added as follows:

Code:
oEvent.pageX = oEvent.clientX + parseInt(document.body.scrollLeft);
oEvent.pageY = oEvent.clientY + parseInt(document.body.scrollTop);
I think this error only happens when using an XHTML doctype, but I'm not sure. The parseInt() makes it work for all setups, regardless of doctype.

There may be other places in the book that need to be updated in the same manner, but this is really the only place I've come across it so far.

Specifically, any place that reads a length property from the style object of an element needs parseInt() added.

For example:

Code:
var x = parseInt(getElementById('sample').style.left);
Hope this helps!

Reply With Quote
  #2 (permalink)  
Old December 28th, 2005, 03:55 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Thanks for the post, but I believe you have things a bit mixed up.

The following all return numeric values:
oEvent.clientX
oEvent.clientY
document.body.scrollLeft
document.body.scrollTop

You are correct in your statement that any value retrieved from the style object must be placed in parseInt() to strip off the units, but none of these values are on the style object and therefore return numbers directly.

Try running just this line of code and you'll see:
Code:
alert(document.body.scrollTop);
So, the event compatibility handler is correct, however, you may be trying to use it in such a way that is wrong. For instance:

Code:
d.style.left = oEvent.pageX;
This will fail when using an XHTML doctype because you need to add the "px":

Code:
d.style.left = oEvent.pageX + "px";
Regards,
Nicholas

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
  #3 (permalink)  
Old December 28th, 2005, 04:07 PM
Registered User
 
Join Date: Dec 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yup, stupid me, I just figured out the problem, and it wasn't parseInt().

It turns out that I needed to change the code to:

(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) instead of just using document.body.scrollTop.

Any idea why document.body.scrollTop is always returning zero?

(Same for scrollLeft)

I'm using IE 6.0.

Many thanks.

BTW, love your book. It's getting quite wrinkled with all the constant use.

Reply With Quote
  #4 (permalink)  
Old December 28th, 2005, 04:09 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Ah yes, quite different.

In strict mode, the HTML element is considered to be the viewport for the page; in quirks mode, the BODY element is considered to be the viewport of the page. Scroll information always is accessible on the viewport, not necessary a given element.

Glad you've enjoyed the book!

Nicholas

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
  #5 (permalink)  
Old December 28th, 2005, 05:11 PM
Registered User
 
Join Date: Dec 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Interesting ... I'm glad to know the reason, because oftentimes you just have to accept that something works a certain way "just because it does". Was I correct that the code in the book should be updated? (Perhaps an errata?)

Reply With Quote
  #6 (permalink)  
Old February 14th, 2006, 09:46 PM
Registered User
 
Join Date: Feb 2006
Location: Tallahassee, Florida, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just spent an hour trying to figure this out... I changed my doctype to XHTML 1.1 strict and all of a sudden it quits working only for IE... I thought it was something I changed in my script... thanks...

Nicholas (Nikk) Madigan Folts (www.nikkfolts.com)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Soap Error in same machine code from book taposh_dr BOOK: Professional WCF Programming: .NET Dev with Windows Communication Found ISBN: 9780470089842 0 July 12th, 2007 03:45 PM
where is c# code for book? djricochet BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 3 April 10th, 2006 10:11 AM
Where is the code of this book! zjut C# 0 October 18th, 2004 11:08 PM
The Code of book ASP.NET Website Programming Error jackahu BOOK: ASP.NET Website Programming Problem-Design-Solution 0 June 16th, 2004 12:04 PM
Where can you get the code for the book jjk8501 Classic ASP Databases 1 September 18th, 2003 08:07 AM



All times are GMT -4. The time now is 02:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.