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 December 2nd, 2012, 10:47 PM
Authorized User
 
Join Date: Aug 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default divs vs tables

Hi Jeremy.

I'm making another of my periodic forays into trying to learn JS. I'm working through your new 24-hour trainer book, which I really like. It seems to have a much clearer theoretical structure than the Beginning JavaScript that you revised for Wilton. Probably the advantage of starting from scratch rather than trying to update something that went back so many years.

Anyway, though, I'm a bit disappointed that you're using tables for your layouts (eg in Lesson 17) rather than the currently more approved divs. I've found getting divs to float and unfloat properly is hard, but that's particularly why I'd like to see some good examples of that approach actually being put to use. I've seen ads for web programmers "who hate tables." Maybe in the second edition?

Also, does everything in the book apply equally to HTML5?

Thanks.
 
Old December 3rd, 2012, 02:32 PM
Authorized User
 
Join Date: Aug 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default divs version

I figured it was worth my while to dig in and practice developing a grid using divs. I came up with the following, using stuff I found on the web. I don't claim to understand how it all works. As someone on the web said, using divs for grid layout seems as much a hack as using table a la Russian dolls. (I added ' ' around each link to give greater clicking space.) The code seems to work fine on Firefox in Linux; haven't checked on other browsers or OSs.

Code:
<!DOCTYPE html>
<html dir="ltr" lang="en-US"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>Lesson 17: Example</title>
    <!--[if IE ]>
     <script> //For production, put this function in a JS script directory
      /*** CSS VQS - Chapter 2 - HTML5forIE.js ***/
(function(){if(!/*@cc_on!a*/0)return;var e= "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog, eventsource,figure,footer, header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','); for(var i=0;i<e.length;i++) {document.createElement(e[i])}))()
    </script>
    <![endif]-->
    
    <style type="text/css">
      #grid {
        width:350px;
        clear:both;
      }
      .row {
        float:left;
        overflow:hidden;
      }
      .inputs {
        float: left;
        border: 1pt gray solid;
        padding:5px;
        overflow:hidden;
        white-space:nowrap;
        margin-right:2px;
        margin-top: 2px;
      }
      #results {height:20px; width:300px;}
      .inputs {float:left; margin:10px; width:50px}
    </style>
</head>
<body>
<div id="grid">
  <div class="row" id="row1" >
    <div class="inputs" id="results">
    
    </div> <!--div#results -->
  </div> <!--div#row1 -->
  <div class="row" id="row2">
     <div class="inputs"><a href="#" onclick="return addDigit(1)"> &nbsp;1&nbsp; </a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit(2)">&nbsp;2&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit(3)">&nbsp;3&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit('+')">&nbsp;+&nbsp;</a></div> 
  </div><!--div#row2 -->
  <div class="row" id="row3">
     <div class="inputs"><a href="#" onclick="return addDigit(4)">&nbsp;4&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit(5)">&nbsp;5&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit(6)">&nbsp;6&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit('-')">&nbsp;-&nbsp;</a></div> 
  </div><!--div#row3" -->
  <div class="row" id="row4">
     <div class="inputs"><a href="#" onclick="return addDigit(7)">&nbsp;7&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit(8)">&nbsp;8&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit(9)">&nbsp;9&nbsp;</a></div> 
     <div class="inputs"><a href="#" onclick="return addDigit('*')">&nbsp;*&nbsp;</a></div> 
  </div> <!-- #div#row4 -->
  <div class="row" id="row5">
      <div class="inputs"><a href="#" onclick="return reset()">Clear</a></div> 
      <div class="inputs"><a href="#" onclick="return addDigit()">&nbsp;0&nbsp;</a></div> 
      <div class="inputs"><a href="#" onclick="return calculate()">&nbsp;=&nbsp;</a></div> 
      <div class="inputs"><a href="#" onclick="return addDigit('/')">&nbsp;/&nbsp;</a></div> 
  </div><!--div#row5 -->
</div><!--div#grid -->
<script>
   function addDigit(digit) {
      var resultField = document.getElementById("results");
      resultField.innerHTML += digit;
      return false;
   }
   
   function calculate() {
     var resultField = document.getElementById("results");
     resultField.innerHTML = eval(resultField.innerHTML);
     return false;
   }
 
   function reset() {
      var resultField = document.getElementById("results")
      resultField.innerHTML = "";
      return false;
   }     

</script>
</body></html>

Last edited by econophil; December 3rd, 2012 at 09:29 PM..
 
Old December 4th, 2012, 01:22 AM
jmcpeak's Avatar
Wrox Author
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

Howdy, econophil.

From a teaching standpoint, I wanted to focus on teaching JavaScript. Going a route that requires extra CSS and/or polyfills adds unwanted complexity to the already complex topic of learning JavaScript (especially in the midst of a discussion on events). Tables are advantageous in this scenario for two reasons:

- they work in every browser
- everyone knows how they work

That allowed me to practically skip over an extra discussion on the markup. Whether or not the tables disappear in a future edition remains to be seen. Thanks for putting in the effort to building a grid =)

I'm afraid I don't fully understand your HTML5 question. Everything in the book works regardless if you're using HTML4, XHTML, or HTML5. Granted, none of the HTML5 APIs or features are covered in the book. A second edition will definitely add that information on those APIs.

Last edited by jmcpeak; December 4th, 2012 at 01:25 AM..
 
Old December 4th, 2012, 09:25 AM
Authorized User
 
Join Date: Aug 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Good points.

Thanks, Jeremy. Those are good points. When I look at the "divs" solution, it's definitely much more complex. (Does that make it "more of a hack" than the "tables" solution?--theological question, I think.)

Regarding HTML5: I wasn't thinking too clearly there, failing to distinguish backward compatibility from new features. And it will be quite a while before everyone's browsers catch up and are HTML5 compatible.
 
Old December 4th, 2012, 12:45 PM
Authorized User
 
Join Date: Aug 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Interesting: the spaces I put around the cell contents (&nbsp;) in the code above interfere with the "standard DOM event handler," lesson 19, version but not with the HTML attribute event handler model (lesson 17). Presumably some subtle difference in how innerHTML is parsed in the two cases.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Tables or divs gujjar CSS Cascading Style Sheets 1 January 9th, 2007 08:13 AM
divs inside divs: theory? Giant_robot_sandwich CSS Cascading Style Sheets 11 September 13th, 2006 03:56 PM
Absolute position vs. other divs beckfield CSS Cascading Style Sheets 2 March 23rd, 2005 04:21 AM
IE5 Problem with divs pgtips HTML Code Clinic 7 February 6th, 2005 02:14 PM





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