473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Resizing font of div won't work in Chrome or Firefox

Hi. I have a javascript which is designed to increase and decrease the text size of a particular div so that reaaders can feel more comfortable. However it works in IE, but not firefox or chrome. My guess is that it could be using some functions that are not supported by those browsers.

Would anyone be able to modify this script so that it works in all browsers?

The javascript:
Expand|Select|Wrap|Line Numbers
  1. /*#######################################*/
  2. /*            Beta FONT SIZER                 */
  3. /*#######################################*/
  4. window.onload = function() {
  5.     _LSfontSizer.init(null);
  6. }
  7.  
  8. origSize = new Array();
  9.  
  10. var _LSfontSizer = {
  11.     contentID:'content',// The outer most element of the text you want to resize
  12.     debug: false,        // Allow alert of tracking values
  13.     enabeled: true,        // Enable the fontsize tool to function single point to disable
  14.     maxChange: 5,         // Max number of increase/decreases in font size
  15.     changeCnt: 0,         // Track change counts
  16.     defaultSize: '14',    // NA - default font size
  17.     maxSize: '20',        // NA - max font size
  18.     minSize: '8',        // NA - minimum font size
  19.     curSize: null,        // loop container for current size and manipulation
  20.     origSizeCnt: 0,        // Track the item change count for reset value mapping
  21.     echo: '',            // debug value outputs
  22.     activeFunc: null,    // Store the active function that was called
  23.     init: function(call){
  24.             this.echo='';
  25.             this.activeFunc = call;
  26.             if(this.activeFunc == null) { // Setup Listeners onload
  27.                 try {
  28.                     Event.add($('fntSizeInc'),'mouseup',function() { _LSfontSizer.init('add'); });
  29.                     Event.add($('fntSizeDec'),'mouseup',function() { _LSfontSizer.init('dec'); });
  30.                     Event.add($('fntSizeReset'),'mouseup',function() { _LSfontSizer.subSetFunc('get'); });
  31.                     if(document.all) {
  32.                         $('fntSizeInc').style.cursor = "hand";
  33.                         $('fntSizeDec').style.cursor = "hand";
  34.                         $('fntSizeReset').style.cursor = "hand";
  35.                     } else {
  36.                         $('fntSizeInc').style.cursor = "pointer";
  37.                         $('fntSizeDec').style.cursor = "pointer";
  38.                         $('fntSizeReset').style.cursor = "pointer";
  39.                     }
  40.                 } catch(e) { 
  41.                     //do nothing since the fontSizer interface is not present
  42.                 }
  43.                 //return; //End
  44.             }
  45.  
  46.             if(this.activeFunc=='add') {
  47.                 if(this.changeCnt+1>this.maxChange)
  48.                     this.enabeled = false;
  49.                 else {
  50.                     this.changeCnt++; this.enabeled = true;
  51.                 }
  52.             } else if (this.activeFunc=='dec') {
  53.                 if(this.changeCnt-1<(0-this.maxChange))
  54.                     this.enabeled = false;
  55.                 else {
  56.                     this.changeCnt--; this.enabeled = true;
  57.                 }
  58.             }
  59.             if(this.enabeled) {
  60.                 bodyObjs = $(this.contentID).childNodes;
  61.                 for(var i=0;i<bodyObjs.length;i++){
  62.                     if(bodyObjs[i].nodeType == 1)  // IE 
  63.                         this.traverseTree(bodyObjs[i])
  64.                     // END nodeType
  65.                 } // END for loop
  66.                 if(this.debug) alert(this.echo);
  67.             }
  68.         },
  69.     set: function(obj) {
  70.             this.curSize = getStyle(obj.parentNode,'font-size');
  71.             if( this.curSize != 'undefined' && obj.nodeName=="#text" && obj.parentNode.nodeName != "DIV") {
  72.                 origSize.push(this.curSize);
  73.                 this.echo += "\n\rSET DEFAULT = " + this.curSize;
  74.             }
  75.         },
  76.     get: function(obj) {
  77.             this.curSize = origSize[this.origSizeCnt];
  78.             if( this.curSize != 'undefined' && obj.nodeName=="#text" && obj.parentNode.nodeName != "DIV") {
  79.                     var newSize = parseInt((this.curSize.substring(0,this.curSize.length-2))) + 1;
  80.                     obj.parentNode.style.fontSize = newSize + this.curSize.substring(this.curSize.length-2,this.curSize.length);
  81.                     this.echo += "\n\rRESET TO DEFAULT:: "+this.curSize+" >> Parent = "+obj.parentNode.nodeName+" >> This = "+obj.nodeName;//+" :: nodeValue "+obj.nodeValue;
  82.                     this.origSizeCnt++;
  83.             }
  84.     },
  85.     add: function(obj) {
  86.             this.curSize = getStyle(obj.parentNode,'font-size');
  87.             if( this.curSize != 'undefined' && obj.nodeName=="#text" && obj.parentNode.nodeName != "DIV") {
  88.                     var newSize = parseInt((this.curSize.substring(0,this.curSize.length-2))) + 1;
  89.                     obj.parentNode.style.fontSize = newSize + this.curSize.substring(this.curSize.length-2,this.curSize.length);
  90.                     this.echo += "\n\rADD:: "+this.curSize+" >> Parent = "+obj.parentNode.nodeName+" >> This = "+obj.nodeName;//+" :: nodeValue "+obj.nodeValue;
  91.                     this.origSizeCnt++;
  92.             }
  93.         },
  94.     dec: function(obj) {
  95.             this.curSize = getStyle(obj.parentNode,'font-size');
  96.             if( this.curSize != 'undefined' && obj.nodeName=="#text" && obj.parentNode.nodeName != "DIV") {
  97.                     var newSize = parseInt((this.curSize.substring(0,this.curSize.length-2))) - 1;
  98.                     obj.parentNode.style.fontSize = newSize + this.curSize.substring(this.curSize.length-2,this.curSize.length);
  99.                     this.echo += "\n\rDEC:: "+this.curSize+" >> Parent = "+obj.parentNode.nodeName+" >> This = "+obj.nodeName;//+" :: nodeValue "+obj.nodeValue;
  100.                     this.origSizeCnt++;
  101.             }
  102.         },
  103.     traverseTree: function(tree){
  104.             var cnt = null;
  105.             if(tree.hasChildNodes() && tree.nodeName != "P") { // Don't process paragraph sub elements
  106.                 var nodes = tree.childNodes.length;
  107.                 for(var i=0; i<tree.childNodes.length; i++) {
  108.                     if(tree.childNodes[i].nodeName != "A"){
  109.                         this.traverseTree(tree.childNodes[i]);
  110.                         cnt++;
  111.                     }
  112.                     this.echo += " :: "+cnt+" :: ";
  113.                 } 
  114.             } else if (tree.nodeName == "P") { // Only pass the outer paragraph tag once to limit is change occurance to one
  115.                 if(this.activeFunc == 'add') 
  116.                     this.add(tree.childNodes[0]);
  117.                 else if (this.activeFunc == 'dec')
  118.                     this.dec(tree.childNodes[0]);
  119.                 else if (this.activeFunc == 'get')
  120.                     this.get(tree.childNodes[0]);
  121.                 else 
  122.                     this.set(tree.childNodes[0]);
  123.             } else {
  124.                 if(this.activeFunc == 'add') 
  125.                     this.add(tree);
  126.                 else if (this.activeFunc == 'dec')
  127.                     this.dec(tree);
  128.                 else if (this.activeFunc == 'get')
  129.                     this.get(tree);
  130.                 else 
  131.                     this.set(tree);
  132.             }
  133.         },
  134.         subSetFunc: function(val) {
  135.             this.origSizeCnt = 0;
  136.             this.echo += "\n\rOrigSize Length = "+ origSize.length;
  137.             for(i=0;i<origSize.length;i++) {
  138.                 this.echo += "\n\rOrigSize["+ i + "] = "+origSize[i];
  139.             }
  140.             this.enabeled = true;
  141.             this.changeCnt = 0;
  142.             this.init(val);
  143.         }
  144. }
  145.  
  146. function $(element) {
  147.     var elements = new Array();
  148.     for (var i=0;i<arguments.length;i++) {
  149.         var element = arguments[i];
  150.         if (typeof element == 'string') element = document.getElementById(element);
  151.         if (arguments.length == 1) return element;
  152.         elements.push(element);
  153.     }
  154.     return elements;
  155. }
  156.  
  157. function getStyle(el,styleProp)
  158. {
  159.     var x = el;
  160.     if (x.currentStyle)
  161.         var y = x.currentStyle['fontSize'];
  162.     else if (window.getComputedStyle)
  163.         try { var y = document.defaultView.getComputedStyle(x,null).getPropertyValue('font-size'); }
  164.         catch(e) {
  165.             //donothing
  166.         }
  167.     return y;
  168. }
  169.  
  170. var Event = {
  171.     add: function(obj,type,fn) {
  172.         if (obj.attachEvent) {
  173.             obj['e'+type+fn] = fn;
  174.             obj[type+fn] = function() { obj['e'+type+fn](window.event); }
  175.             obj.attachEvent('on'+type,obj[type+fn]);
  176.         } else
  177.         obj.addEventListener(type,fn,false);
  178.     },
  179.     remove: function(obj,type,fn) {
  180.         if (obj.detachEvent) {
  181.             obj.detachEvent('on'+type,obj[type+fn]);
  182.             obj[type+fn] = null;
  183.         } else
  184.         obj.removeEventListener(type,fn,false);
  185.     }
  186. }
The html code:
Expand|Select|Wrap|Line Numbers
  1. <img src="fnt_sizer_sub.gif" id="fntSizeDec" alt="decrease font size" />
  2. <span id="fntSizeReset" style="padding:0 3px;" >Font Resize</span>
  3. <img src="fnt_sizer_add.gif" alt="Increase font size"  id="fntSizeInc" />
  4.  
  5.  
  6. <div id="content">
  7. <p>This is pages font..I want to resize it.</p>
  8. </div>
Sep 23 '10 #1
1 2171
dlite922
1,584 Expert 1GB
Is this your script? Looks like you grabbed it from somewhere and looking to get some free code to make it compatible with other browsers.

You don't need all that to change font size. you could do it in two lines of code in javascript. Just edit the CSS!

Expand|Select|Wrap|Line Numbers
  1.  
  2. function increaseSize()
  3. {
  4.   var curSize =   parseInt(document.getElementById('mydiv').style.fontSize);
  5.   curSize = ; 
  6.   document.getElementById('mydiv').style.fontSize = (curSize+5)+'px'; 
  7. {
  8.  
  9.  
I have not tested the above code. It's just to give you an idea.


Dan
Sep 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Martin | last post by:
I have this simple little script that submits a page where some server-side scripting deletes a record from a database. This works as expected in IE but it appears that, in FF, the submit is not...
4
by: request | last post by:
Hi I am trying server request/response program using socket class. here i couldn't send headers with the request to the proxy server.when i send the request i should able to get the response from...
3
by: Steve Yerkes | last post by:
There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There...
1
by: SteveS | last post by:
Hello. This problem is perplexing me. I have a asp:button which runs a javascript function when it is clicked. It returns a true or false depending if a postback is needed. This works great...
12
by: raghu | last post by:
Hello Everyone, I have a doubt for a long time. consider a structure struct A{ int z; } struct n{ struct A *a; int *y; }*st;
1
by: Miroslav Stampar [MCSD.NET / Security+] | last post by:
I simply want to get event property inside a function. Inside Firefox this works fine, but in IE event is 'undefined' (in this sample) Code snippet: <html> <body> <script> function...
9
by: v4vijayakumar | last post by:
Why this works?! #include <stdio.h> int main() { const char * str = "123" "456" "\n";
55
by: Kenny McCormack | last post by:
#include <stdio.h> int main(void) { printf("hello, world\n"); } I compile it with all warning turned on and it still works fine.
6
by: oneadvent | last post by:
This code works in Chrome and pulls a second drop down, but it will not work in IE6, (I dont have IE7/8 to check right now, but can later) and it works in FF, in IE it doesn't do anything at all. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.