New Posts

Monday, August 30, 2010

Javascript 101: The Basics

Today we'll start our Journey down the wondrous world of client side scripting.
Javascript is an object oriented interpreted language used to give websites a bit more dynamism in the form of user interactions and interface usefulness that gives the website more ease of use.

However Javascript can and frequently is overused, or misused. We'll look at how a little further into this post.

First of all what does Javascript being object oriented and interpreted mean to the web site developer.

Its interpreted, which means you don't need to compile it, as most browsers automatically understand it and interpret it on the fly.

Its also Object Oriented which means its designed to work off of little black boxes called objects that have properties and methods. That is they have traits to them and can do things.

Everything in JS is an object From the browser window, to the HTML button tag they are all objects which have properties that define them like height, width and a name, and methods; things they can do, like the click() method of a button which clicks the button and executes any associated actions.

For the Browser to understand that Javascript is present in a page it can be in 1 of 3 locations.

Between script tags
In an event of an element in the page such as onClick.
Or preceded by the javascript protocol in a link reference or address section < a href="javascript:someFunction();">

When writing Js code, you will be using objects to access and manipulate things on your page, the principal object you will be dealing with the most is the document object.

This object contains most of the other elements and functions you may want or need to access. From page elements to action events.

Its most useful functions are the getElementBy functions. These functions allow you to find an element in a web page and be able top manipulate its properties and methods.

These are getElementById, getElementsbyName , and getElementsByTagName


getElementById allows you to find any element by its ID. It returns a single element you can use, and if erroneously there is more than one element with the same ID it will only return the first element it finds.

getElementsByName returns an array of all elements that have a matching name.

getElementsByTagName returns an array of all the element of a specific type. Say all Divs all inputs, all images etc..

For instance:

var myElement=document.getElementById('theID');
This returns a reference or pointer if you will to the object or element with an id of 'theID'


To exemplify lets say you have a textbox in your page you want to manipulate when a button is pressed:


<input type=text id="myTextBox" value="Sample">

<input type=button name="manipulate" value="Manipulate Text Box">


First we need to determine when the button is pressed for that we use one of the buttons events. Events are actions that occur to the object. An action such as when the button itself is clicked would trigger the onClick event of the object. So we want to have something happen at that time.

Lets say we want to alter the contents of the text box when the button is clicked. so we use the onClick event of the button to have it execute our JS function to change the value of the text box:


<input type=button name="manipulate" id="MyTxtBox" value="Manipulate Text Box"
onClick="changeTxtBox();">


So know we've set our button to execute a function when its pressed, but what would our function look like and where should it go:

Usually our Javascript code should reside in the script section of our page, which goes in between the <head></head> tags.


<html>
<head>
<script type="text/javascript">

function changeTxtBox(){
...
}
</script>
</head>



Notice how the name of your function matches exactly what we placed in the onClick event of our button. Javascript is case sensitive which means uppercase letters are treated differently to lower case letters. so FunctionName would be different to functionname.


So now that we have our function we need to access our textbox and modify its contents. We'll want to access our element using the getElementById method of the document object and passing the ID we gave to our text box "MyTxtBox".



var OurTextBox=document.getElementById('MyTxtBox');


So we now have a reference to our text box in OurTextBox variable. From here we can manipulate the text box as require. Since we want to change the contents of th textbox which currently says "Example" we'll use its value property:


OurTextBox.value="This is the replaced value";



So at the end our function look like:


function changeTxtBox(){
var OurTextBox=document.getElementById('MyTxtBox');
OurTextBox.value="This is the replaced value";
}


And that's it. Our Button will now alter the contents every time it is clicked.

Try it out here, write something into the textbox and press the button.



By the same toke we can access many other properties of the object such as width, height, name etc...




Javascript 102: Reinventing the Wheel



There are many things that can be done with JS, but as I've said before just because you can do something doesn't mean you should.

JS is supposed help you provide a more pleasant experience to the user, but not at the cost of bugs, and strange issues because your code is so complex you have a hard time maintaining it.

If there's already an HTML element for what you want to do its always a good idea to use it rather that to try to reinvent the wheel.

The most common "Wheel Reinvention" I've encountered is of course the lowly link.
Using a button, or some other html element to simulate a link by using JS.

A few examples follow. A Div, a button, an img, and last one is an actual link.
Notice how all of them look pretty much the same, even the picture-less image. There is no point in using JS to make those things act like a link when there's already a link element.



This is a simple example which only required a single line of JS, but you may encounter other times when you want to do something when there's already something there that does it.




Javascript 103: Error Checking



When coding in Javascript its important to check for errors, as you'll find that for the most part a website will lad without complaining about Javascript issues, but your actions may not function properly.

But how does one check for error when none are shown? Simple you request them. Most Browsers offer some type of debugger to let you know that something went wrong with your client side code.

  1. Internet Explorer shows its very conspicuous Gold shield on the bottom left corner of its window. Double clicking there will bring up the debugger window when there are errors.

  2. Firefox has its Error Console which can be accessed from the Tools menu

  3. Safari has an Error Console under its Develop menu which needs to be activated from its Advanced Tab in the Preferences Dialog.



From there you can see any and all errors you may have listed, and attempt to correct them.

Monday, August 16, 2010

DSL Speed and Downloads

This is slightly tangential to the Web development side of things but its interesting to note none the less since it affects how fast the website will have downloaded to the client machine, and be ready for our users to ... well.. use.


I was going to download a Mandriva Linux ISO image to install, and I noticed by download speed was only around 30KBps (Kilobytes per second) which considering my Internet connection is clocked at 4Mb (4 Megabits) by my ISP was rather strange. I figured out I was downloading from a far away mirror server, once I changed to a closer one, the download speed multiplied to around 350KBps. So I went from a download that was going to take 16 hours, to one that took only 30 minutes to complete.

But how can you tell your speed is not right, well that's what we will be talking about today.

When the internet first became publicly accessible, we had modems with blazing speeds of 9600 Kb/s, then 14400 Kb/s until they reached the modem cap of 56Kbps. Websites were pretty much text based up until the latter days of 36K modems when we started to get more and heavier graphics, flash animations made their debut, and videos and audios starting to trickle into the web consciousness.

But alas, the modems were sadly outclassed because they just weren't fast enough to keep up with the demands of the content. At this point Web developers had to keep a careful eye on how big the site would end up being, how many images were going to have to be downloaded, and how big these files were because the speed of the modems was limited, and you wouldn't want to have your users waiting for the page to load.

Today speeds have multiplied at least 10 times what they were then, but they still cause the same amount of confusion.

ISP's will tell you you have xMb (Megabits) of blazing speed but how does that actually translate into how fast your downloads will be, or how quickly your Youtube videos will start playing. In practice downloads are measured in bytes, while ISP's will use bits to promote their blazing bandwidths.

Lets take a regular 56k modem. That's 56000 bits per second of data transfer.
Since 1 byte equals 8 bits that modem was transferring at a "blazing" speed of 7000 bytes per second or about 7 kilobytes per second. For a reference, a regular text file is about 1 or 2KB. Low res images range in 30 to 50 KB range while mid range images have file sizes in the hundreds of kilobytes, and hi res images move into the thousands of kilobytes or what we know as Megabytes. So a website, with pictures, audio and text quickly starts to add up the download time. A single 200KB image which by today's standards is pretty small, would take about 30 seconds to download through our 56K modem. So for half a minute you'd sit there waiting for the picture to come up. Granted back then 200KB files were considered somewhat large so most WB developers had smaller images sacrificing quality, but imagine a website with more pictures, and code etc... you'd be waiting for a while before you had a fully downloaded website.

Fast forward a few yeas and we get MegaBit speed plans. These DSL plans basically tell you your plan will have a download speed of up to the specified number. That is X will be the maximum speed yo can expect from the connection. However, due to the way DSL works, the speed can't be guaranteed so you'll have a variance of speed. Still there should be an operational margin where you can feel you are getting what you are paying for.

Say your DSL is offering you a 4Mb Plan at $19.99. That's 4,000,000 (Four million)
bits per second maximum data transfer or 488 KiloBytes per second. You should be expecting a bit less than that, somewhere between 250 to 400 KBps; depending on where you are downloading from, is acceptable. Anything less and you can argue you are in the next lowest price package at 2Mb. It may vary from time to time, but as long as it stays more consistently in that range your o.k.

Online DSL speed tests calculate this by measuring how long it takes for you to download a specifically sized file.

If you want to try out your speed, here are a few sites that offer speed tests.



Monday, June 14, 2010

The :hover Pseudo class

Lets get into something a bit more interesting now. You build your website, and you find you need some way to say hide parts of your navigation, you need to hide info until requested, heck you may even just want to keep say a small picture of an item hidden until your mouseover a little camera icon.

This can be accomplished with CSS alone.

We will need to use the :hover CSS pseudo class. A pseudo class is a special type of CSS definition, if you will, that allows you to give an object certain effects when the mouse interacts with it in some way. For instance have an area show up when the mouse is hovered over an element.

To get the effect we want, we'll apply the hover pseudo class to our div or section that we wish to hide or show when the mouse hovers over it.


Lets look at a basic example:


CSS



#basic{
border:1px solid #DDDDDD;
background-color:#CADBED;
width:300px;
height:23px;
overflow:hidden;
}


#basic:hover{
height:auto;
}



Html



<div id='basic'>
Hover Here <br>
This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.
</div>





Hover Here


This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.





Move your mouse over the "Hover Here" text to see it in action. This is a very basic collapsible panel. It uses the hover Pseudo Class and changes only the height definition of the div from the set 30px; to auto so it automatically grows to fit its content.




Of course you can get a bit fancier if you need to, and give the panel a nicer header so you clearly separate the title from the contents.



CSS



#hideaway{
border:1px solid #DDDDDD;
background-color:#CADBED;
width:300px;
height:23px;
overflow:hidden;
}

#hideaway h3{
margin:0px;
padding:0px;
font-family:Verdana;
background-color:#E6DFC8;
border-bottom:1px solid #CCCCCC;
}


#hideaway:hover{
height:auto;

}




Html



<div id='hideaway'>
<h3>Hover Mouse over this Area</h3>
This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.
</div>




Hover Mouse over this Area


This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.
This content will be seen when hovering over the
titlebar. you could not see it before.




This one now has a header to hover over, and a defined content section.




There are many things to be done with the Hover pseudo class.

Say you have a list of Products in a table and you wish to add a popup thumbnail of the product image to for instance:

Hover over the camera icon to see a pic pop up.







ProductDescriptionPricePicture
Product 1This is an average Product$29.99
thumbnail
Product 2This is a great Product$9.99
thumbnail
Product 3This is an expenive Product$99.99
thumbnail



CSS



.thumb{
background:url('/images/cameraico48.png');
width:48px;
height:48px;
}

.thumb img{
width:128px;
height:128px;
display:none;
}
.thumb:hover{
background:url('none');
}

.thumb:hover img{
position:absolute;
display:block;
}



<table border=0 style="border:1px solid #DDDDDD;"><tr><td>Product</td><td>Description</td><td>Price</td><td>Picture</td></tr>

<tr><td>Product 1</td><td>This is an average Product</td><td>$29.99</td><td><div class="thumb"><img src="/images/picicon.png" alt="thumbnail"></div></td></tr>

<tr><td>Product 2</td><td>This is a great Product</td><td>$9.99</td><td><div class="thumb"><img src="/images/picicon.png" alt="thumbnail"></div></td></tr>

<tr><td>Product 3</td><td>This is an expenive Product</td><td>$99.99</td><td><div class="thumb"><img src="/images/picicon.png" alt="thumbnail"></div></td></tr>

</table>







Lets move onto Navigation. When you are creating menus, you may have options in the menu, and maybe some sub options you wish to group together under an option, but you do;t necessarily need to have them displayed all he time. You can use the hover pseudo class to make your menu a bit more dynamic.

For example, if you hover over options 2 and 4 you'll get extra options displayed:



Now the Code and CSS at work:


CSS



#nav{
border:2px solid #DDDDDD;
background-color:#C3D6CB;
width:180px;
}

#nav li{
height:20px;
}

#nav li ul{
display:none;
}

#nav li:hover{
color:#FFFFFF;
height:auto;
border:1px solid #AAAAAA;
}

#nav li:hover ul{
display:block;
background-color:#108541;
}

#nav li li:hover{
background-color:#14E36A;

}




Html



<div id="nav">
<ul>
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a>

<ul>
<li><a href="#">sub Option 1</a></li>
<li><a href="#">sub Option 2</a></li>

<li><a href="#">sub Option 3</a></li>
</ul>
</li>
<li><a href="#">Option 3</a></li>

<li><a href="#">Option 4</a>
<ul>
<li><a href="#">sub Option 1</a></li>

<li><a href="#">sub Option 2</a></li>
<li><a href="#">sub Option 3</a></li>
</ul>

</li>
</ul>
</div>




This is getting quite complex, s lets take a closer look at what the CSS is doing:

First I have the main navigation container, that will hold my options. Then I simply specify a starting width for my nav options using the list tag.

This is where it gets interesting:

#nav li ul{
display:none;
}

Here I specify that any ul tags inside a list tag that is inside an element with an ID of nav should not be displayed to begin with.

#nav li:hover{
color:#FFFFFF;
height:auto;
border:1px solid #AAAAAA;
}

#nav li:hover ul{
display:block;
background-color:#108541;
}

Now the hover pseudo class comes into play. First I just use it to change the color of the hovered option so we know its being hovered.

The second line, specifies what to do with the originally hidden ul that holds the sub option, when the list item they are inside of is hovered. That is they become visible.


Now you can of course style the items as you see fit, removing the bullet, changing fonts etc.. to suit your needs but this is the basic functionality.




For our final example lets suppose we have a horizontal menu, and we need to give it drop down menus, that don't actually push the content under them down as the appear.

For this we will need to actually use the position attribute. I'll go into more depth about this attribute in a later post, but for now, you should now, that unless it is absolutely necessary to be used, the position attribute should not be used. This is because it changes how elements are treated and can cause lots of confusion with its results. Once you know what it does and how to work with it it can be useful, but again its not something you'll want to use all over the page.

Lets look at our drop down example now:


File

  • Option 1
  • Option 2
  • Option 3
  • Option 4

 




CSS



#nav2{
height:30px;
width:350px;
border:1px solid #AAAAAA;
background-color:#777777;
position:absolute;
}

#nav2 div{
float:left;
background-color:#777777;
height:28px;
overflow:hidden;
width:116px;
}

#nav2 div:hover{
background-color:#000000;
color:#FFFFFF;
height:auto;
}

#nav2 p{
clear:both;
}

#nav2 div h3{
margin:0;
padding:0;
text-align:center;
}

#nav2 div.dropdown li:hover{
background-color:#FFFFFF;
color:#000000;


}


Html



<div id="nav2">
<div class="dropdown">
<h3>File</h3>
<ul>
<li>Option 1</li>

<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
</ul>

</div>
<div class="dropdown">
<h3>Edit</h3>
<ul>
<li>Option 1</li>
<li>Option 2</li>

<li>Option 3</li>
<li>Option 4</li>
</ul>
</div>

<div class="dropdown">

<h3>View</h3>
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>

<li>Option 4</li>
</ul>
</div>
<p>&nbsp</p>
</div>
<br>

<br>




This as you can see takes quite a bit more CSS to get it to function.
Notice the single use of the position:absolute; attribute for the nav2 container which will hold our menus. This prevents the drop downs from pushing on the content below them, and instead just open up above it as one would expect.

Basically all I do is float our menu divs, and have them change heights when hovered over so they can show the sub options with in them.

This are just a few examples of how useful the hover pseudo class is in modern browsers.

Now there is a caveat here. IE6, and you know it had to be that particular browser, does not support the hover pseudo class in anything but link tags.

<a href="#">Option 4</a>

So all these techniques fail miserably. Thankfully, users are moving away from IE6, and as I mentioned in a previous post the percentage of users still working on IE6 is so small its almost not worth going out of your way to code something that will work on it.

If for some reason the examples don't work correctly here you can click here to see all the examples in action.

Sunday, May 30, 2010

Tables and Validation

Evil Markup? Maybe not


Tables... The very word sends shivers down any self proclaimed CSS master. Tables are evil, tables should never be used. Tables should be cast into oblivion from this day forth. Baloney!!

Tables like every other tag in HTML have its uses. However like anything in this world it can be misused. Most Web Designers use applications such as Photoshop, or what have you, to design the look of their site, and then have this same application transform it into HTML. Yes it may be HTML it may even look nice in some cases, but is it correct? Is it Valid? Probably not.

Most applications use tables to transform the image rendition of a layout created by a designer into HTML, however these applications will normally use obsolete, difficult and hard to maintain ways of building a website. Like I mentioned in my previous POST, A simple layout is easier to maintain. Most of the time they'll rely on tables to create the layout because that was the technique used years ago. however as the Web has progressed so have the techniques to design and build websites.
A simple 3 column layout like we saw before ends up a mis-mash of cells and columns instead of a nice fluid understandable piece of code.

Tables

<table>
<tr><td colspan=3>Header area</td></tr>
<tr> <td> Left Column</td>
<td>Center Column with lots of content...</td>
<td>Right Column</td>
</tr>
</table>


Vs DIV's


<div id="header"></div>
<div id="leftcol"></div>
<div id="contentcol"></div>
<div id="rightcol"></div>


7 Lines of code vs 4. And this is just a basic skeleton. Now suppose you want to change the look, like we did in the previous post. Move Navigation to be horizontal, you'd need to go in and alter the entire markup. Now its seems reasonably simple without any content, but imagine tons of content in the middle column, and trying to find where the right column begins after all that content with more tags and text etc...

With the proper use of CSS you couldn't care less what the content of the DIV is where it starts or where it ends. You look at your CSS and you modify that.

Now I must make a confession, calling your columns left, right and center is not a good idea, because like with tables you give them a predetermined location or layout position, and as I showed with CSS, they really don't need nor should they have one.

So for an accurate representation, you could call them navigation, content, and maybe "adbar" or extra_content pane ro something that is descriptive of its contents, but not of its position in the page.

If you need to change its location, style, dimensions etc.. it you can look for their style definitions in the style sheet and change those. So Navigation moves to the top horizontally, "adbar" may end up at the bottom heck you could even set it as the left column instead. All without altering the markup.

You don't have a left column, you have a navigation "pane" for lack of a better word, and you can place it anywhere you want to.

All this without altering your base markup. Do it with tables, and you'll be starting from scratch every time.

This does not immediately mean tables no longer have a place in the world of Web development and should never again be used or heard of. Tables play an important role if used properly.

But how do you use tables properly? You use them to display tabular data.
But now, what is tabular data? Well, it can be many things. It can be a directory, it can be a product listing, it can be a roster sheet for a baseball league etc... All that can be considered tabular data. The common factor? Its data, its listings, its information that has that particular display need and structure, and will not change. In this context tables become a tag to markup how data is to be structured, not where its going to be on the page, or what its going to look like.

htm,example,table


<table border=0 cellspacing=0 cellpadding=0 style="border:1px solid #999999;">
<tr><td colspan=3>Players</td></tr>
<tr> <td> Name</td>
<td>Position</td>
<td>Email</td>

</tr>
<tr><td>Peter</td><td>Midfielder</td><td>pete@baseballnuts.com</td></tr>
<tr><td>John</td><td>Rightfielder</td><td>john@baseballnuts.com</td></tr>
<tr><td>Frank</td><td>Pitcher</td><td>frank@baseballnuts.com</td></tr>
<tr><td>Jason</td><td>First Baseman</td><td>jason@baseballnuts.com</td></tr>
</table>


Validation



Validation is simply the product of adhering to the rules and norms set by the W3C for design.

As long as your coding follows those rules it can be considered valid, and stands a good chance of looking the same across all the different platforms.
But hey!, we all make mistakes, and we make even more when we are coding a website. So to help us out the W3C offers a service to check your web pages and make sure it conforms to the set of rules you've specified in your webpage as your Doctype. If you made a mistake and missed something it will tell you and often offer an explanation of the errors.

The W3C's validator is located here:
http://validator.w3.org/

You can use it to run your pages through and make sure there are no errors.
web,markup,css,html

It has 3 ways of checking your website:

  1. By URL

  2. By File Upload

  3. By Direct Input



By Url is the most direct, you give it a URL or address where it can access your website online, and it verifies it.

By File Upload: Takes your base file directly. Reads it and returns its result.

By Direct Input,: By far the easiest option, just copy and paste your entire code, and press the button.

Options 2 and 3 are only useful if your file is 100% pure HTML. If you are working with a server side language like PHP, ASP or Cold Fusion, then its often better to offer the URL. Otherwise the server side code is not processed, and may cause issues that aren't really there in the final page.

Lets take our example website we used in the last post and give it a whirl:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Structure Vs Presentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>
<body>

<div id="header">
<h1>This is the Main Website Title</h1>
</div>
<div id="navigation">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
</ul>
</div>
<div id="content">
<h3>This is Content Title</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin odio turpis, hendrerit bibendum fringilla sit amet, lacinia sit amet lacus. Quisque varius tellus vel libero tempor laoreet. Suspendisse vel dignissim tellus. Etiam fringilla consequat eros, in volutpat lectus molestie at. Vivamus vitae mi justo, quis gravida nisi. Tristique senectus et netus et malesuada fames ac turpis egestas...
</p>
<div id='subcontent'>
<p>
Suspendisse potenti. Duis pellentesque, sem varius facilisis vehicula, lectus purus semper mauris, quis laoreet metus sem vel diam. Ut non tellus nulla. Sed egestas dui ac nulla eleifend vulputate. Sed blandit lacus eu arcu luctus et condimentum erat scelerisque...
</p>
<p>
Pellentesque mauris assa, aliquet vel volutpat sed, facilisis vitae urna. Vestibulum consequat metus id magna mollis auctor. Nunc id tellus nec sem auctor convallis ut a quam. Suspendisse vel urna velit, feugiat ultrices leo. Vestibulum orci ipsum, venenatis quis ultrices at, varius sed sem. Nulla facilisi. In porta, nunc vel rutrum adipiscing, dui lectus accumsan... </p>
</div>
<p>
Quisque sit amet dui libero, at pharetra erat. Nullam ante mauris, hendrerit vel accumsan sed, porttitor et purus. Fusce interdum sodales justo, a pretium velit porta eu. In hac habitasse platea dictumst. Etiam semper laoreet dolor quis pharetra. Integer id est vel tortor iaculis laoreet congue non nibh. Cras non dui massa. Sed nulla tortor, volutpat vitae accumsan eget, imperdiet quis nibh. In accums...
</p>
</div>

</body>
</html>

We'll use the Direct Input option, and just copy and paste our website inot the text area as seen in the pic above. Press the button and the Validator Returns 2 Warnings, (about the same thing actually). Our Example Markup is considered valid.
valid,html,wrc,result

The 2 warnings refer to the lack of character encoding. Its an additional line that tells the browser what encoding to use when displaying the page. Most servers send a predefined header for this, and unless you need a specific encoding you can get by with the standard encoding.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


I'll get into it more in a future post. For now its just a lesser warning we may safely ignore.

Let's introduce a real error shall we:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Structure Vs Presentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>
<body>

<div id="header">
<h1>This is the Main Website Title</h1>
</div>
<div id="navigation">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
</ul>
</div>
<div id="content">
<h3>This is Content Title</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin odio turpis, hendrerit bibendum fringilla sit amet, lacinia sit amet lacus. Quisque varius tellus vel libero tempor laoreet. Suspendisse vel dignissim tellus. Etiam fringilla consequat eros, in volutpat lectus molestie at. Vivamus vitae mi justo, quis gravida nisi. Tristique senectus et netus et malesuada fames ac turpis egestas...
</p>
<div id='subcontent'>
<p>
Suspendisse potenti. Duis pellentesque, sem varius facilisis vehicula, lectus purus semper mauris, quis laoreet metus sem vel diam. Ut non tellus nulla. Sed egestas dui ac nulla eleifend vulputate. Sed blandit lacus eu arcu luctus et condimentum erat scelerisque...
</p>
<p>
Pellentesque mauris assa, aliquet vel volutpat sed, facilisis vitae urna. Vestibulum consequat metus id magna mollis auctor. Nunc id tellus nec sem auctor convallis ut a quam. Suspendisse vel urna velit, feugiat ultrices leo. Vestibulum orci ipsum, venenatis quis ultrices at, varius sed sem. Nulla facilisi. In porta, nunc vel rutrum adipiscing, dui lectus accumsan... </p>
</div>
<p>
Quisque sit amet dui libero, at pharetra erat. Nullam ante mauris, hendrerit vel accumsan sed, porttitor et purus. Fusce interdum sodales justo, a pretium velit porta eu. In hac habitasse platea dictumst. Etiam semper laoreet dolor quis pharetra. Integer id est vel tortor iaculis laoreet congue non nibh. Cras non dui massa. Sed nulla tortor, volutpat vitae accumsan eget, imperdiet quis nibh. In accums...
</p>
</div>
<img src="myimage.jpg">
</body>
</html>


I've added an image. But I neglected to include one of the properties that it requires.

When we run it through the Validator, it spits out a big nasty red error:

validator,w3c,error,alt

And here we have its explanation:
validator,error,wrc,img,alt


Line 38, Column 21: required attribute "ALT" not specified





The attribute given above is required for an element that you've used, but you have omitted it. For instance, in most HTML and XHTML document types the "type" attribute is required on the "script" element and the "alt" attribute is required for the "img" element.


It tells me the line number, the code around the error, and why it may be wrong.
Now I can go back to my code and fix it:


<img src="myimage.jpg" alt="This is an Image" >


Should we run it again, it will validate.

At the end and like with many other things, correct usage of tags for their intended purposes will help produce a nicer looking website that is easier to maintain.


Well that's it for my second entry into this Blog. Hope you enjoyed it.

Saturday, May 15, 2010

Welcome to Web 101

Welcome to Behind the Web or as I like to call it Web 101. My name is Phil B, I'm a IT consultant and specialize in Web development. Specifically applications involving HTML PHP, and Javascript.

I've created this Blog as away of opening up the web development world to people who want to lear about having a website that is easy to maintain and attractive and comfortable to browse.

I've seen through my years many websites that use overly complex markup to present the simplest of designs. When simpler, easier to understand markups can do the same in less time, and consume less resources for the visitor.

In today's World Wide Web we have to cater to a variety of people, Operating systems, and web browsers. This can make it difficult to create a website that looks the same across all these different platforms. The more complex the markup is, the more likely something is going to break, or fail to look the same for everybody.

I'm also of the idea that a simpler site is a better site. I find sites that have all these transitions and effects and things that change and animate around to be kind of annoying. I also completely despise sites that abuse Flash animations.
Flash can be used as an intro movie to the site if required, but basing the entire site around Flash is just overkill.

Additionally Flash based websites alienate an important part of the browsing population: The mobile population.

The Mobile Population are those people that use their phones, PDA's or other mobile devices to access websites. Well if you are one of the many users (like me) of iPhones, or iPod Touches, and now iPads then you know Flash based sites are a big no no because these devices have no Flash support. At least 30% of the mobile users use one of these devices. That means a Flash based site just turned down 30 out of every 100 users. That's a lot of people that can't see the site at all. Add to that the devices that have limited Flash support and there's no guarantee all the features in your Flash site will work. I've tried to navigate through different flash based sites with mobile devices such as Nokia phones, and there's always some type of issue preventing the full functionality of the site. You might be losing 50% of your target audience because of Flash.

Simpler, easier cleaner sites, have a better chance of looking right in mobile browsers. This also has the benefit of allowing you to design a single site instead of 2 or 3 versions for different devices.

The first thing to know when developing a website is that Internet Explorer is almost always wrong in how it renders elements. Sadly Microsoft's own browser still doesn't know how to properly render websites even in their current version 8 browser. Fortunately the statistics show that the usage of IE is declining rapidly. From 2006 where IE6 garnered about 60% of all browser users to a mere 19% now for IE8.

IE's disability can be overcome with properly coded websites.




The first step to a site that works across most platforms is creating Valid HTML, CSS, JS etc.. What is valid code? Valid code is code that adheres to certain standards established by a group called the World Wide Web Consortium or W3C for short. They determine the set of rules and norms that are to be followed to create a site. Valid code follows these rules and guarantees a consistent look across the different platforms.

One must also let the browser know what these rules are so it can render the content appropriately. These rules are contained in files called Doctypes. Short for Document Types these files basically list the rules the browser should follow to render or display the page. Setting this at the top of your page means that any browser loading your page will know how its supposed to render it. This keeps things consistent. Even IE follows these rules when properly set.

The most common Doctype is the Transitional Doctype.

This gives you the most leeway in coding, by giving you access to presentational and stylistic elements within the markup like font tags and other deprecated tags. Other doctypes like the Strict doctype disallow such tags, and demand all presentational and stylistic coding to be done through CSS.


There are other doctypes that can be used in other specific circumstances but for our purposes will stick mostly with Transitional, and Strict.

What this approach wants to achieve is to separate the presentation from the structure of the document. The structure refers to how the information is organized. You have paragraphs, lists, sections etc.. The Presentation is how those structure elements look. Do they have a border? A background? Are they 10 or 50 pixels tall? etc...

Though as mentioned before, the Transitional doctype lets you have the presentational elements within markup, its always a good idea to avoid their placement there as much as possible.

Here's an example of how a full separation of presentation and structure can make having completely different looking websites with the same base markup a simple task.

Base Markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Structure Vs Presentation</title>

</head>
<body>

<div id="header">
<h1>This is the Main Website Title</h1>
</div>
<div id="navigation">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
</ul>
</div>
<div id="content">
<h3>This is Content Title</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin odio turpis, hendrerit bibendum fringilla sit amet, lacinia sit amet lacus. Quisque varius tellus vel libero tempor laoreet. Suspendisse vel dignissim tellus. Etiam fringilla consequat eros, in volutpat lectus molestie at. Vivamus vitae mi justo, quis gravida nisi. Tristique senectus et netus et malesuada fames ac turpis egestas...
</p>
<div id='subcontent'>
<p>
Suspendisse potenti. Duis pellentesque, sem varius facilisis vehicula, lectus purus semper mauris, quis laoreet metus sem vel diam. Ut non tellus nulla. Sed egestas dui ac nulla eleifend vulputate. Sed blandit lacus eu arcu luctus et condimentum erat scelerisque...
</p>
<p>
Pellentesque mauris assa, aliquet vel volutpat sed, facilisis vitae urna. Vestibulum consequat metus id magna mollis auctor. Nunc id tellus nec sem auctor convallis ut a quam. Suspendisse vel urna velit, feugiat ultrices leo. Vestibulum orci ipsum, venenatis quis ultrices at, varius sed sem. Nulla facilisi. In porta, nunc vel rutrum adipiscing, dui lectus accumsan... </p>
</div>
<p>
Quisque sit amet dui libero, at pharetra erat. Nullam ante mauris, hendrerit vel accumsan sed, porttitor et purus. Fusce interdum sodales justo, a pretium velit porta eu. In hac habitasse platea dictumst. Etiam semper laoreet dolor quis pharetra. Integer id est vel tortor iaculis laoreet congue non nibh. Cras non dui massa. Sed nulla tortor, volutpat vitae accumsan eget, imperdiet quis nibh. In accums...
</p>
</div>

</body>
</html>

Our base markup consists of a header bar, a navigation section, a content section, and a sub content section inside the larger content area.

Without any CSS applied to it it looks like this:
web,markup,css,html

That's a very plain website, but as you can see its completely usable. Everything works, and can be used. But suppose we want to give it a little flair. We want the links to be on the left side as a column, and the content in the middle, and maybe even a narrower column on the right. Well CSS can do all that, without touching the basic markup.

Lets add a Stylesheet to our markup, basically a set of instructions of how the markup is supposed to be presented to the user.

This is the stylesheet:


*{
margin:0px;
padding:0px;
border:0px;
}

body{
width:100%;
height:100%;
background-color:#DDDDAA;
}
#header{
width:100%;
height:120px;
background-color:#DDDDFF;
}

#navigation{
float:left;
width:15%;
background-color:#DDDDAA;
text-align:center;
}

#content{
width:78%;
float:left;
text-align:justify;
background-color:#FFFFFF;
}

#content p{
width:80%;
margin-left:auto;
margin-right:auto;
margin-bottom:5px;
}

#content #subcontent{

border:2px inset gray;
background-color:#666666;
color:#FFFFFF;
}

But how do we link it to our markup? There are 3 ways to accomplish this:

  1. Stylesheet Reference
  2. In file Style Section
  3. Inline Styles



Number 1, the one we'll use for this example, is basically a secondary file that is linked to our base HTML file.

We link the stylesheet to our HTML markup file with a LINK tag:



We place this link in our html file, between the head tags of our markup.

<head>
<title>Structure Vs Presentation</title>
<LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
</head>



Lets see how our page looks now:

websitem,CSS,HTML,markup


Look at that, we now have a navigation bar on the left, a colored header bar, and our content section with a defined subsection in dark gray, and the markup is still the same.


Lets try a slightly different stylesheet:

*{
margin:0px;
padding:0px;
border:0px;
}

body{
width:100%;
height:100%;

}
#header{
width:100%;
height:120px;
background-color:#DDDDFF;
}

#navigation{
width:100%;
height:20px;
background-color:#DDDDAA;
text-align:center;
}

#navigation li{
float:left;
width:80px;
height:100%;
}

#content{
text-align:justify;

}

#content p{
width:80%;
margin-left:auto;
margin-right:auto;
margin-bottom:5px;
clear:both;
}

#content #subcontent{
margin-left:auto;
margin-right:auto;
width:40%;
height:200px;
overflow:auto;
border:2px inset gray;
background-color:#666666;
color:#FFFFFF;
}



web,markup,css,html

Now we have a horizontal navigation bar between the header and the content, and we have a scrollable sub section inside our main content area. But the main HTML markup remains the same.

That's the beauty, anyone can look at the code, and immediately understand what each section is, and then just apply their own CSS to make it look however they want to.

This is of course a very basic example, but it shows the power of CSS. And how a simple structure can have different layout designs.

Well that's it for my very first Web development post.

I hope you enjoyed this as much as I enjoyed writing it. I'll be going into some much cooler CSS techniques in up coming blog posts, before moving on to Javascript and finally PHP.

Don't forget to comment.




Appendix


For basic coding, any text editor will suffice. My personal favorites are Notepad for Windows, and TextWrangler for Mac.