Pages

Banner 468

Sunday 27 February 2011

Week 2 - Basic Javascript and more CSS

0 comments
 
Hello again.  This weeks task focuses around using javascript and css to create dynamic web pages.  We started with some source code for creating a basic loan calculator and the main aim was to add some more functionality and styling to make it more visually appealing.  In this post I shall be discussing how I went about adding the necessary functionality as well as demonstrating some basic techniques for adding 'themes' or dynamic styles to HTML content.

Analysing the source code
The first thing I did was analyse the source code to understand how the calculator worked.   Basically the user inputs the loan amount, the interest rate and repayment period.  Using this information the monthly repayment, total cost of the loan as well as the total amount of interest paid is calculated.   Behind the scenes, the javascript on the page uses the Document Object Model to access the values input, performs the required calculations and displays the results to the user by changing the innerHTML property of the corresponding <span> elements,  fairly simple.  I also noticed that whenever one of the input values is changed, the loan is re-calculated automatically.  This works because the “onchange” event of each of these input fields was triggering the “calculate()” function.  

Functional Enhancements
After understanding how the calculator worked, it was time to add some more functionality.  The first thing I wanted to do was add a checkbox to the input fields such that whenever it was checked, a 10% discount on the interest rate was applied before calculating the loan.  I then added the following condition to the calculate() function to calculate the discount whenever the checkbox was checked. 


 I also wanted to make sure that whenever the value of the checkbox changed the loan was automatically re-calculated.  To achieve this I simply wired the checkbox's onclick event to the calculate() function.

Next, I wanted to display the total number of installments for the loan as well as the date of the last repayment.  The number of installments was already being calculated so displaying it was a simple matter of creating a new <span> called "installments" and changing its innerHTML property during the calculation.  Displaying the date of the last repayment took slightly more work.  First I created a function that uses javascript's Date object to calculate the loan end date:


At line 118 of the above source code I am multiplying the value of the 'years' input field by 1 to make sure it is converted to an integer before being added to the year variable.  Since javascript is an 'untyped' language there is a risk that the values are treated as strings and consequently the '+' operator would concatenate the values rather than adding them!  Finally I used the toDateString() function to format the date.

Visual Enhancements
With the functionality ready, it was time to make the calculator more visually appealing.  For this particular task, I wanted to go a step further than simply adding the necessary styling.  I wanted to add the ability to change the calculator's style dynamically using javascript.  To do this I used a combination techniques such as leveraging css selectors and css sprites.  From the outset I decided I wanted only one CSS file, and that changing the style should not require re-loading the page.  To do this I used css selectors such that every element on the page was styled based on the CSS class of the <body> tag.  To further explain this, consider the following CSS snippet:

 Lines 7 and 8 show the two schemes (scheme1 and scheme2) that can be applied to the <body> tag.  Next, lines 12 and 13 show how the style of the H1 and H3 elements changes based on the scheme.  Whenever scheme1 is active, H1 and H3 elements would be white in colour, but would be grey if scheme2 was applied.  This concept was applied to the rest of the styles and allows me to change the entire look of the calculator by simply changing the value of the <body> tag's class attribute. The following code snippet shows how the scheme is changed according to the value of a drop-down list box:

Lines 101 and 102 define two arrays to store different text for the H1 and H3 elements such that their text changes depending on the active scheme.  Line 106 is where the scheme actually changes depending on the value of a drop-down list box.  Finally I wired the onchange event of the drop-down list to this function.  Here's how the calculator looks in both colour schemes:





CSS Sprites


Each scheme uses three images, one for the title bar and two for the button (normal and hover states).  Instead of saving each image as a separate file I decided to combine the 6 images into two "master" images, one for the titles and the other for the buttons as shown below:


 I then used the background-position CSS property to display the correct image, as follows:



Line 29 specifies that the "form-title" has a background image and is exactly 40 pixels in height.  Line 32 specifies that the background position of the image should be 0px both horizontally and vertically effectively showing the blue title bar only.  When scheme2 is applied however, the background position shifts the image 40 pixels vertically (line 33) to display the yellow title bar.  The fact that the title bar is defined as being exactly 40 pixels high makes sure that the rest of the image is masked from view.  A similar approach was taken with the buttons, shifting the background position both horizontally (for state) and vertically (for scheme).  The main advantage of CSS sprites is that they lower the amount of HTTP requests being made which makes the page load faster.
Readmore...
Saturday 19 February 2011

Hello World!

0 comments
 
Apologies for the cliche, but this being my first ever blog post I thought it was quite suitable.
My name is Wayne Zammit and I'm currently studying for a degree in Internet Application Development with the University of Middlesex.  The purpose of this weekly blog is to share thoughts on the practical work I'll be carrying out during the "Developing Web Applications" module of this course.

So, with the introduction out of the way it's time to go on to this week's topic: HTML & CSS.
This first post will focus around what I learnt from an old friend of mine: "CSSZenGarden.com".  I first came across this website several years ago while I was learning CSS and without any shadow of doubt, it was (and still is) my best mentor.  Like many things in life, you can only really learn CSS if you get your hands dirty and try things out.  CSSZenGarden.com is all about demonstrating what can be done when there is total separation between content and layout.

Way back when I started developing websites, like most fledgling web developers I based my web pages on HTML table layouts.  They were (relatively) simple to develop, rendered well across browsers and basically got the job done...until a client decided to move that left-hand side menu to the right-hand side of the page!  A simple and reasonable request that was far from simple to accommodate as it meant changing every table in every web page, a daunting and time-consuming task to say the least.  This was a time when classic ASP was not yet classic (it was all the rage) and things like Master Pages were still unheard of.  It was just a horrible mix of 'include' files, page content, server side code and styling...a mess that somehow translated to a website.  I wasn't going to do the same mistake twice and decided it was time to get serious with CSS and really learn it, especially how to achieve table-less layouts.  That's when I came across CSSZenGarden.com and I never looked back. 

Now although the notion of separating content from layout sounds simple, in my experience it rarely is. A lot of thought has to be given as to how the page will be structured into components that might need to be independently styled, now or in the future.  For instance, it might sound sensible to group your website banner and top navigation into the same <div> element called "page-top" but what if that top navigation became a side-bar some time down  the line? You would need to re-group the top navigation into another <div> such that it could be positioned independently of the banner.  That might still mean changing a lot of web pages depending how they are structured.  This might sound fairly obvious but it's often taken for granted and might have an impact on the overall cost of the change. 

There are other more subtle issues such as how to name your styles.  In the previous example the element was called 'page-top' which implies that the element is positioned at the top of the page. That sounds fairly logical but what if that element were to be re-positioned somewhere other than the top of the page?  The style name would become very misleading, especially to new developers who are maintaining the page or style for the first time.  Something like "page-banner"  would make more sense.  I also find that naming styles this way helps me to identify the various components that define the structure of a web page.

However, the worst problem I faced when transitioning to CSS layouts was getting a page to render correctly across browsers.  Mozilla browsers and IE (up to ver.6) had very different implementations of the "box model" (interpretation of element width and padding) which meant a lot of ugly CSS hacks to get the page to look the same on both browser families.  Thankfully today there is much more coherence in the way browsers interpret CSS and most browsers have debugging utilities that help developers get the job done faster. 

Separating content from layout however, has many advantages: maintenance costs are dramatically reduced;  web pages load faster because they are smaller in size and also become much more accessible as they are easier for screen readers to interpret.  It is also makes it much simpler for websites to become "sinkable", where visitors can personalise the look of a website to their liking. Ultimately the end result is well worth the effort!
Readmore...