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 2nd edition ISBN: 9780470227800
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 2nd Edition by Nicholas C Zakas; ISBN: 9780470227800
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 27th, 2010, 04:23 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Best Practice Chapter Question

Early on in this chapter it states that you should decouple HTML and JavaScript from one another, and it specifically states this in relation to using innerHtml to insert a chunk of HTML into the page. However later on in the same chapter under the section 'Optimize DOM Interactions' it states you should use innerHtml for DOM manipulation over DOM methods such a createElement and appendChild as it's faster. Doing this however contradicts the previous best practice of decoupling HTML and JavaScript. So which best practice should I stick to?
  #2 (permalink)  
Old September 28th, 2010, 05:10 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

This isn't a contradiction: one is talking about performance, the other is talking about maintainability. You definitely shouldn't embed HTML into your JavaScript code - that's absolutely true. That doesn't mean you can use innerHTML, though, it just means you shouldn't keep your HTML in the JavaScript where you're using innerHTML. So instead of this:

Code:
element.innerHTML = "<p>Hello world!</p>";
You would read the HTML string from somewhere else (perhaps via an Ajax call), store it in a variable, and do this:

Code:
element.innerHTML = codeToAdd;
__________________
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
[Help]Chapter 10 Practice 4 00Raiser BOOK: Beginning Java 2, JDK 5 Edition 2 October 30th, 2010 07:02 AM
Problems to practice with? kotozy Excel VBA 1 December 29th, 2009 12:21 AM
An error for AJAX practice wen BOOK: Beginning Ajax with ASP.NET 0 December 13th, 2006 06:02 PM
practice help with C++ mastrgamr C++ Programming 2 November 2nd, 2006 07:55 AM
Looking for some practice Vano2005 C++ Programming 2 July 7th, 2005 04:30 PM



All times are GMT -4. The time now is 11:50 PM.


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