How to Make Text Resizer Using HTML JavaScript

Users of a particular web page are sometimes bothered with the font size especially when they have problems with eyesight. Yet, this problems can be solved through this Text Resizer.
Text Resizer enables your user to change the font size in your website. The script is easy and works in any web page. You just have to add to any page one of the CSS classes (on the element itself). This process changes the font size of the website depending on the setting clicked on.
It is important for every page to have a Text Resizer for users to configure their desired font-size of the text.


Directions:

First: Kindly add this code below to the HEAD section of your page.
  1. <link href="css/style.css" rel="stylesheet" type="text/css" />
  2. <script type="text/javascript" src="js/script.js">
  3. </script>
This is the file for <link href="css/style.css" rel="stylesheet" type="text/css" />(style.css)
  1. .x_small{ /*CSS for "extra small font" setting*/
  2. font-size: 11px;
  3. }
  4. .small{ /*CSS for "small font" setting*/
  5. font-size: 13px;
  6. }
  7. .normal{ /*CSS to return page to default setting (with no additional CSS rules added)*/
  8. }
  9. .large{ /*CSS for "large font" setting*/
  10. font-size: 21px;
  11. }
  12. .x_large{ /*CSS for "extra large font" setting*/
  13. font-size: 24px;
  14. }
  15. a.texttoggler{ /*CSS for Text Size Toggler control*/
  16. margin-right: 6px;
  17. }
  18. a.texttoggler img{ /*CSS for Text Size Toggler control*/
  19. border: 1px solid gray;
  20. }
  21. a.texttoggler img:hover{ /*CSS for Text Size Toggler control*/
  22. border: 1px solid red;
  23. }
  24. a.selectedtoggler img{ /*CSS for Selected Text Size Toggler control*/
  25. border: 1px solid red;
  26. }
This one for the <script type="text/javascript" src="js/script.js"></script>(script.js).
  1. var documenttextsizer={
  2. prevcontrol: '', //remember last control clicked on/ selected
  3. existingclasses: '',
  4. setpageclass:function(control, newclass){
  5. if (this.prevcontrol!='')
  6. this.css(this.prevcontrol, 'selectedtoggler', 'remove') //de-select previous control, by removing 'selectedtoggler' from it
  7. document.documentElement.className=this.existingclasses+' '+newclass //apply new class to document
  8. this.css(control, 'selectedtoggler', 'add') //select current control
  9. this.setCookie('pagesetting', newclass, 5) //remember new class added to document for 5 days
  10. this.prevcontrol=control
  11. },
  12. css:function(el, targetclass, action){
  13. var needle=new RegExp("(^|\\s+)"+targetclass+"($|\\s+)", "ig")
  14. if (action=="check")
  15. return needle.test(el.className)
  16. else if (action=="remove")
  17. el.className=el.className.replace(needle, "")
  18. else if (action=="add")
  19. el.className+=" "+targetclass
  20. },
  21. getCookie:function(Name){
  22. var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
  23. if (document.cookie.match(re)) //if cookie found
  24. return document.cookie.match(re)[0].split("=")[1] //return its value
  25. return null
  26. },
  27. setCookie:function(name, value, days){
  28. if (typeof days!="undefined"){ //if set persistent cookie
  29. var expireDate = new Date()
  30. var expstring=expireDate.setDate(expireDate.getDate()+days)
  31. document.cookie = name+"="+value+"; path=/; expires="+expireDate.toGMTString()
  32. }
  33. else //else if this is a session only cookie
  34. document.cookie = name+"="+value
  35. },
  36. setup:function(targetclass){
  37. this.existingclasses=document.documentElement.className //store existing CSS classes on HTML element, if any
  38. var persistedsetting=this.getCookie('pagesetting')
  39. var alllinks=document.getElementsByTagName("a")
  40. for (var i=0; i<alllinks.length; i++){
  41. if (this.css(alllinks[i], targetclass, "check")){
  42. if (alllinks[i].getAttribute("rel")==persistedsetting) //if this control's rel attribute matches persisted doc CSS class name
  43. this.setpageclass(alllinks[i], alllinks[i].getAttribute("rel")) //apply persisted class to document
  44. alllinks[i].onclick=function(){
  45. documenttextsizer.setpageclass(this, this.getAttribute("rel"))
  46. return false
  47. }
  48. }
  49. }
  50. }
  51. }
Second: Just simply add the control code to the BODY section of your page to toggle the size of the text, with 5 different text sizes offered.
  1. <p>Select your desired text size.</p>
  2. <a href="#" class="texttoggler" rel="x_small" title="extra small size"><img src="image/x_small.png" /></a>
  3. <a href="#" class="texttoggler" rel="small" title="small size"><img src="image/small.png" /></a>
  4. <a href="#" class="texttoggler" rel="normal" title="normal size"><img src="image/normal.png" /></a>
  5. <a href="#" class="texttoggler" rel="large" title="large size"><img src="image/large.png" /></a>
  6. <a href="#" class="texttoggler" rel="x_large" title="extra large size"><img src="image/x_large.png" /></a>
  7. <p>Do you have source code, articles, tutorials, web links,
  8. <br />and books to share? You can write your own content here.
  9. <br />You can even have your own blog. Submit now. </p>
  1. <script type="text/javascript">
  2. documenttextsizer.setup("texttoggler")
  3. </script>
This reference of 5 sample images used to controls the text size inside the sourcecode, which you can download below. Kindly click the ("Download Code").
How this script works?
This script works by applying to the document's in the element, the CSS class name specified in the text size control the user clicks on. The controls are simply links with a rel attribute pointing to the CSS class you want to apply in the document. We are here 5 sample controls:
  1. <a href="#" class="texttoggler" rel="x_small" title="extra small size"><img src="image/x_small.png" /></a>
  2. <a href="#" class="texttoggler" rel="small" title="small size"><img src="image/small.png" /></a>
  3. <a href="#" class="texttoggler" rel="normal" title="normal size"><img src="image/normal.png" /></a>
  4. <a href="#" class="texttoggler" rel="large" title="large size"><img src="image/large.png" /></a>
  5. <a href="#" class="texttoggler" rel="x_large" title="extra large size"><img src="image/x_large.png" /></a>
By clicking the first control, it applies the CSS class x_small to the <HTML> element of your page and other controls. Which defines the CSS classes referenced by your controls:
  1. .x_small{ /*CSS for "extra small font" setting*/
  2. font-size: 11px;
  3. }
  4. .small{ /*CSS for "small font" setting*/
  5. font-size: 13px;
  6. }
  7. .normal{ /*CSS to return page to default setting (with no additional CSS rules added)*/
  8. }
  9. .large{ /*CSS for "large font" setting*/
  10. font-size: 21px;
  11. }
  12. .x_large{ /*CSS for "extra large font" setting*/
  13. font-size: 24px;
  14. }
And that's all!!! The user can set his/her desired font-size document of a current site using the controls.
I hope this simple work, may help you in your project.
Practice, and enjoy coding!!!

Write Comment...
EmoticonEmoticon