In this article, you'll learn that it requires only a few lines of Java Script to let the visitors to your Web site control the font size. You'll also learn how to use a cookie to store their preference on their computer.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Let Your Web site Visitors Set Font Size

Why should you let visitors to your Web site set the font size? According to the U.S. Census Bureau, adults 50 plus will grow to over 106 million by 2015, when they will account for 45 percent of the adult population. Not only is the population aging, but adults 50 plus are also the fastest growing segment on the Internet. If your Web site is directed toward seniors, or people with visual impairment, giving your Visitors a way to set the font size is crucial.

In this article, you'll learn that it requires only a few lines of Java Script to let the visitors to your Web site control the font size. You'll also learn how to use a cookie to store their preference on their computer. You'll also learn how to set the cookie so that their one time font size preference will apply to every page on your website, and when they leave your Web site, they won't have to set it again when they return.

• Before you proceed, consider what impact changing the font size may have on your webpage layout. In some cases, this may cause your layout to stretch and distort. You may need to go back and set absolute dimensions for some elements.

The first step is to add the attributes shown below to the <body> tag of your webpage.

<body id="myBody" onLoad="setFont();">

The first attribute assigns an id to the body element. This will also work with other webpage elements like a div, span, or table. The second attribute makes the onLoad event of the body tag execute a function named setFont. This function will be used to set the initial font size every time a webpage loads. The code for the setFont function is shown below. Paste this Java Script code block into the <head> section of your webpage.

<script type="text/javascript">

var sizeFont = "=12px";

function setFont()
{
var setSize = sizeFont;
document.getElementById("myBody").style.fontSize = 
   setSize.substring( setSize.indexOf("=") + 1, setSize.indexOf("x") + 1  );
}

</script>

This code creates a global variable named sizeFont and initializes it to the string value "=12px". The setFont function creates a variable named setSize and initializes it with the value in sizeFont. It then uses the String object's substring method to extract the substring "12px" from setSize, and uses the document's getElementById method to set the size of the font.

The second step is to place the two links shown below at the top of the <body> section of your webpage.

<a href="javascript:changeFontSize(1)">Bigger Font</a>
<a href="javascript:changeFontSize(-1)">Smaller Font</a>

• You could get fancy and make these links into styled buttons or graphic image buttons.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Slide Show Code
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using Java Script
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269