Archive: Javascript

FontSizer Reloaded - Changing Font Sizes With JavaScript

Posted on Friday, November 9th, 2007

I broke this script and have fixed it! Please read about it and download the script from this post.

A long time ago I wrote a script that changes font sizes. In the year since, I have learned a lot about JavaScript and accessibility and looking over the code I wrote I was ashamed! I will dissect the mistakes I made in another post, but for now I have re-written and vastly improved the script.

Improved Functionality, Improved Accessibility

The script now does everything for you without you having to change your markup, so it degrades nicely when JavaScript isn’t available and it uses (mostly) standards based JavaScript (but don’t get me started on Internet Explorer support for setAttribute).

Once the DOM has loaded the script will check to see if the font has already been set by trying to read a cookie (cookie script was originally from Quirksmode). If the cookie is found then the font size of the body element is set to the previous value. If no cookie is found then the script creates an element and measures the height of it to work out the current font size (thanks to a script on detecting font resizing at A List Apart) and sets that as the cookie. It then adds three links to the document, appending them to an element of your choice. The three links increase, decrease and reset the font size, saving the changes to the cookie.

How To Use It

Now all you need to do to use the fontSizer is:

  1. Download the script (there is an updated version with class names below)
  2. Change the string “fontControls” at the very end of the script to the element you want the font sizing links appended to.
  3. Upload the script to your server
  4. Include the following line in the <head> of your document:
    <script type="text/javascript" src="fontSizer.js">
    </script>

When you visit the page you did this on, you should find that three links appear: A+, R and A- (they’re not pretty, but customisation is left as an exercise*). On clicking A+ the font size of the whole document will increase, clicking A- will decrease the font size and R will reset it to the original size. The script has been tested and works in the following browsers:

  • Internet Explorer 5.5, 6 and 7
  • Firefox 2
  • Opera 9
  • Safari 2
  • Camino 1.5

I have also provided a (very simple) example. So, if you want to add an unobtrusive, accessible way of increasing your website’s font size, please download the fontSizer script and use it!

If there are any problems, please let me know and I will do my best to help you out.

As requested in the comments, I have updated the fontSizer to include class names for styling the links. The “A+”, “R” and “A-” will now come with the classes “increaseSize”, “resetSize” and “decreaseSize” respectively. If you want to change them, just have a look into the source code and you should see where they are set. The new version is available to download now.

* I have always wanted to say that!

Speeding Up Unobtrusive JavaScript By Beating window.onload

Posted on Tuesday, November 6th, 2007

I am looking to rework a couple of my scripts here at Unintentionally Blank as in the time since writing them I have learned a lot about accessible, unobtrusive JavaScript. My old scripts no longer cut it and it is time to improve them. Before I start, I wanted to beat window.onload, the event that tells your JavaScript functions that everything has loaded and you can use the DOM now. I had heard of solutions to this before, but now I was to start developing these scripts again I wanted to find the best solution.

Why Take On window.onload

Unobtrusive JavaScript relies on externalising all your JavaScript code and calling it in via <script> declarations in your HTML document’s <head>. With this method, adding functions to elements in the document needs to be done by attaching event handlers which classically was done with window.onload. However, window.onload only fires once the document and any elements in the document have loaded. So, if you have large images or lots of them the window.onload event will not fire until everything has loaded. This has the potential of leaving any elements you want to give events to go without them, potentially allowing users to click on these elements and not receive the enhanced behaviour that your JavaScript was just waiting to provide.

Beating window.onload by adding event handlers when the DOM, but not necessarily the content and images, has loaded reduces this time and quickens the page’s response.

The Solutions

Looking around the internet threw up a number of increasingly complicated solutions. Dean Edwards seemed to come up with the original solution which was steadily improved until a final version as released (and subsequently used in the JQuery library, I believe). Peter Michaux followed up with some increasingly complicated stuff to remove the time between the page becoming visible and the events being attached, but the Dean Edwards method seems to work for most applications on a small scale.

Following in the footsteps of Dean and packaging the function up into a standalone solution was Jesse Skinner with his addDOMLoadEvent. Not only did this implement the solution nicely, with all the updates that have occurred since the original solving, Jesse also compressed the function as best as possible. The current version stands at only 671 bytes in size and works in Internet Explorer, Firefox, Opera 9 and Safari, plus degrades nicely to use window.onload for any browsers that don’t support the technique.

Coming up soon then, will be the return of my scripts, faster, more accessible and generally better than ever before thanks to this and other techniques I have picked up over the last year. It will be like an early Christmas present, I can’t wait!

Learning AJAX: Lesson 4 - Using The XMLHttpRequest To Get Your Data

Posted on Friday, November 2nd, 2007

Time to finish off my short series on simple AJAX. I started a while ago with an explanation on not using AJAX, then I backed that up with an unobtrusive way of adding a link to a page using JavaScript so that we only use AJAX when JavaScript is available. In the last post I explained, simply, the XMLHttpRequest and now I will show you what to do with it, culminating in the completion of a very simple, AJAX powered random quote generator.

Continue reading “Learning AJAX: Lesson 4 - Using The XMLHttpRequest To Get Your Data” »

Web Accessibility — Javascript

Posted on Thursday, October 18th, 2007

Continuing in my series of posts on different things to think about when considering web accessibility this week I want to look at JavaScript.

I actually had some inspiration in this post from my girlfriend. I accidentally left JavaScript disabled one night after doing some testing on a site and came home the next day to be told there was “something wrong with the internet.” I quickly realised what had happened and rectified the situation, but the sites that she thought had broken may surprise you. Three major ones caught my attention and there’s a fourth I’d like to mention as well.

Continue reading “Web Accessibility — Javascript” »

Javascript: Getting IE To Return this

Posted on Thursday, September 20th, 2007

Internet Explorer can make the simplest piece of code hard. It turns a simple function into a headache. The standards say one thing, but Internet Explorer doesn’t just disobey those standards, it comes up with a different function and a different idea of what that standard even meant. All I want is that when you click on an element to trigger a function, you can refer to the element using this. Let me explain.

Simple Javascript Functions… Really?

I am gaining a lot of experience and practice with javascript in my new job and I am really enjoying it. I’ve dealt with some complex stuff and some seemingly simple stuff. But sometimes it is the simplest of functions that cause the biggest problems.

All I needed to do was create a page with a few <textarea>s that held text for someone to copy. To aid in this, I was to write a short javascript function that would automatically select the text when the <textarea> was clicked on. Here is the function that will do it:

function selectText() {
this.focus();
this.select();
}

Attach selectText() to the onClick event for each <textarea> and I was sorted.

Except For One Thing…

Internet Explorer, in all its wisdom, returns the window object as the result of this regardless of the element that triggered the event. Firefox and all other sane browsers return the element as you would expect. Quirksmode explains this much better than I can.

I spent forever searching for a way to return the element that triggered the event so that I could use the function, finally stumbling across a mention of event.srcElement in a forum. Looking it up, I discovered that this was the function I needed. However, using it took a more roundabout method than hoped.

When attaching an event to an element, you can only give the reference to the function, passing parameters is not allowed. To pass event.srcElement to the event that I was attaching I needed an anonymous function. Let me illustrate with code:

For standards compliant browsers:

textareas[i].addEventListener(’click’, selectText, false);

For Internet Explorer:

textareas[i].attachEvent(’onclick’,function () {
IEselectText(event.srcElement);
});

Got There In The End

Here is the code I finally produced, with a little example of how it works.

So, if you want to use this don’t forget event.srcElement.

(Unless there’s a much better way, but I spent a good while searching for this and I didn’t get anything better!)


cheap online viagra
viagra 25 mg order
cialis 10mg
100 mg cialis us pharmacy
viagra and cialas
generic levitra wholesale 100mg
viagra by phone
buy viagra online 50mg
buy cheap p viagra
cheap viagra online order viagra now
cheap viagra online uk
buy real viagra pharmacy online
cheapest price for viagra
cheapest generic substitute viagra
buy cheap online prescription viagra
buy cheapest viagra
rx cialis 100mg
canada pharmacy discounted viagra 100
generic cialis wholesale 100mg
cialis 50 mg
cheap viagra uks
discount drugs cialis 100mg
buy viagra on the internet
viagra by mail canada
100mg levitra
cheapest generic silagra viagra
cheep generic viagra
cheapest place to buy viagra online
buy levitra online viagra
buy generic viagra online
cheap 25mg levitra
viagra 100mg price
buy kamagra viagra india
cheap cialis canada
buy levitra online
cheap viagra canada
cheap viagra st
cialis 50mg online
viagra and cialis and
buy from pharmacy us viagra
viagra brand
viagra buy oonline
viagra brands
buy get online prescription viagra
cheapest viagra
buy levitra viagra
buy levitra viagra online
buy online prescription viagra without
10 generic viagra for 19.95
buy cheapest online place viagra
cialis 10
buy say viagra
cheap phizer viagra
cialis 2.5
buy generic online viagra
buy levitra canada
cialis dosage 20mg
cheap online softtabs viagra
levitra no prescription
50 mg viagra retail price
buy site viagra
viagra buy general
viagra online prescription
order levitra online
rx viagra 100mg
buy cheap cialis
buy cheap viagra online now uk
buy cheap online viagra viagra
cialis 100mg price
buy discount cialis
cheap overnight viagra
20mg cialis
5mg cialis generic
buy cheap viagra on the net
buy real viagra online
viagra by the pill
cheapest generic viagra
buy kamagra viagra
cheap viagra uk
cheap viagra online pharmacy online
cialis 20mg
buy online drug viagra pharmacy
buy 10 mg cialis
buy sale viagra
buy viagra online and get prescription
cialis black reviews
cheapest uk viagra
viagra and cialis cheap
buy online prescription viagra
buy in online usa viagra
buy viagra no prescription
buy cheap viagra in uk
buy in uk viagra
cheapest place to buy viagra
buy keyword viagra
buy cheap viagra online now
generic viagra wholesale 100mg
cheap order prescription viagra
buy levitra wighout prescription
buy cheap cialis generic levitra viagra
cheap online viagra viagra
cialis no prescription
cheap online pill viagra
purchase levitra online
buy prescription viagra without
cialis 10mg
levitra 100mg price
viagra buy viagra online
viagra by overnight delivery
cheapest viagra in the uk
cheapest place buy viagra online
cialis 20 mg
buy pharmaceutical viagra
buy cialis no prescription
buy online pill viagra
buy cheap deal pill viagra
250 mg cialis
buy real viagra online pharmacy
viagra buy generic
cheap levitra online
cheap site viagra
buy free viagra viagra
buy viagra online
buy cialis online 50mg
viagra buying
cialis 10 mg
buy viagra wighout prescription
viagra 25 mg
cheapest prescription viagra
buy cialis 50mg
viagra 50mg
40 grams of cialis
levitra 10mg 20mg
cialis 24
buy levitra no prescription
buy cheap viagra online u
cheap price viagra
buy discount viagra
buy pharmacy pill viagra
viagra without prescription
cheap 25mg cialis
cheap discount levitra
viagra brazil
buy cheap viagra on
levitra 50mg
cheap online price price viagra
buy viagra online at cheap price
viagra buy australia
buy generic viagra
cheap order site viagra
buy now levitra
cialis 25 mg order
levitra 50mg online
buy cialis online
cialis reviews
buy cialis wighout prescription
cheapest generic viagra and canada
buy cheap viagra online
cialis 30
buy line viagra
buy generic viagra online pharmacy online
levitra 25 mg order
cheap online order viagra
cheapest uk supplier viagra
250 mg viagra
viagra buy viagra
buy online sale viagra viagra
cialis 25 mg
cheapest generic price viagra
cialis 2005
viagra 50mg online
buy in online uk viagra
cheap levitra online
viagra by money order
buy cheap viagra prescription online
buy cialis online now
cheapest generic viagra and cialis pills
cheapest prices for viagra online
50mg cialis
cialis 2005
viagra by mail order
buy cialis internet
cialis 5mg
buy cheap levitra
buy cheap sale viagra
viagra and coupon
levitra 20 mg
viagra 10mg 20mg
cialis 10mg 20mg
buy cheap discount levitra
buy now online viagra
usa cialis
buy online price viagra
buy online levitra cialis viagra
100 mg viagra us pharmacy
viagra buy now pay later
buy cialis no prescription
cheapest price for generic viagra
cheapest regalis viagra
viagra 100mg
cheap levitra canada
buy levitra 50mg
cialis 50mg
purchase levitra
cialis 20
national bingo night
cheap online sales viagra
buy online purchase viagra
viagra buy ionline
cheap viagra online
cheap pill pill sale viagra
buy cialis online viagra
cheapest online viagra
buy free viagra on internet
50 mg cialis retail price
levitra online
order levitra
cheap soft tab viagra
viagra buy uk
cialis 10 mg
buy pfizer viagra
cialis black 800mg
buy prescription viagra
buy viagra on-line
generic cialis
cheap online generic viagra
buy online online viagra viagra
buy p viagra
10mg cialis
cialis 20mg
buy viagra online a href
viagra buying online
20 mg cialis
cheap prescription viagra without
cialis without prescription
order 50mg cialis
cheap pharmacy viagra
buy cheapest online viagra
buy cheap cialis
buy generic viagra usa
cheap viagra without prescription
cheap viagra online prescription
allegra
viagra by mail catalog
voagra online without prescription
buy online uk viagra
buy online online pill viagra viagra
cheapest in uk viagra
canada pharmacy discounted levitra 100
free bingo
50 mg viagra
buy cheap viagra
36 hour levitra
tamiflu
buy viagra online alternative viagra
buy generic viagra img
buy online sale viagra
cialis 20
generic viagra 20mg pills erections
50 mg levitra
viagra buy do nu
viagra buy it online now
cialis 30mg
buy now viagra
cheep viagra
levitra 50 mg
order 50mg viagra
buy discount levitra
buy online pharmacy viagra
cheapest line viagra
cheap 25mg viagra
buy line viagra where
cialis 32
buy 100 mg viagra
10 generic cialis for 19.95
buy viagra onli
buy cheap viagra online uk
buy discount levitra
cheapest price on viagra
cheap cialis canada
levitra brand levitra 100mg
viagra 50 mg
buy generic no online prescription viagra
cheap online purchase viagra
buy deal deal price viagra
buy price viagra
buy cheapest viagra online
buy generic viagra buy
cialis without prescription
buy viagra 50mg
buy cheap viagra uk
buy prescription vaniqa viagra
levitra 25 mg
viagra buy
cheapest prices on generic viagra
cialis 5m tablets
buy cheap online uk viagra
buy cheap generic viagra
voagra online without prescription
buy pill price price viagra
buy discount propecia
cialis 100mg
viagra by mail
cialis 36 hours
buy cheap online viagra
cheap pharmaceutical viagra
cialis 50mg
buy later now pay viagra
buy cheap uk viagra
buy cheap viagra cheap viagra online
cialis online
buy cheap generic online viagra
viagra no prescription
buy locally viagra
buy file viagra
generic levitra 100mg
cialis 10
buy free online sale viagra viagra
cheapest price viagra
order 50mg levitra
cialis brand cialis 100mg
buy real viagra
cheapest generic viagra and cialis
generic cialis 20mg pills erections
cheapest cialis
rx levitra 100mg
10mg viagra
buy cheap viagra
cheapest site viagra
buy cheap site viagra
cialis 20 mg
levitra 100mg
viagra buy in uk online
generic levitra 20mg pills erections
discount drugs levitra 100mg
cheap cialis canada
buy online order viagra
discount drugs viagra 100mg
cialis 1
viagra 125 mg
online slots
cheap cialis canada
buy levitra now
buy generic viagra pharmacy online
36 hour viagra
cheapest cheap viagra
cialis online prescription
100mg viagra
voagra online without prescription
100mg cialis
buy cheap deal viagra viagra viagra
buy low price viagra
buy in spain viagra
levitra online prescription
canada pharmacy discounted cialis 100
buy pill viagra
250 mg levitra
cialis 20mg reviews
buy cialis now
buy generic viagra viagra
cialis 125 mg
levitra without prescription