suzie Posted April 9, 2010 Share Posted April 9, 2010 Dear,Am using "Plus" and "minus" buttons on my website, under the article, to let visitors increase and decrease the fontam usibg this javascript function:function decreaseFontSize() { var f = document.getElementsByTagName('h4'); for(i=0;i<f.length;i++) { if(f.style.fontSize) { var s = parseInt(f.style.fontSize.replace("px","")); } else { var s = 12; } if(s!=12) { s -= 1; } f.style.fontSize =s + "px"; }}it works well, but in IE8,..in the status bar it gives me error on page, with the yellow exclamation Mark...and this appear when I mouse over the button plus or minus."Object expected, and it indicates the line f.style.fontSize = s + "px";call to the function :<td width="20" bgcolor="#FFFFFF"><a href="java script:decreaseFontSize();" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image19','','images/minus-red.gif',1)"><img src="images/minus.gif" name="Image19" border="0" id="Image19"/></a></td>please any Help, how to get rid from this error (N.B: the function works very well) Link to comment Share on other sites More sharing options...
jeffman Posted April 9, 2010 Share Posted April 9, 2010 You must not be using a strict doctype, or that function will fail everywhere. Remember why you are using parseInt(). Without it, you get a string that ends in "px", right? That's because the .style property expects string assignments. Try this, which will automatically cast your number to a string and add the suffix: f[i].style.fontSize = s + "px"; I'm guessing that is the problem, anyway. Link to comment Share on other sites More sharing options...
suzie Posted April 9, 2010 Author Share Posted April 9, 2010 thanks for your reply,no it's not the case...I removed the (+ "px")..to try if it is the problem...but I put it again,so what do you think ? Link to comment Share on other sites More sharing options...
jeffman Posted April 9, 2010 Share Posted April 9, 2010 Try to alert the value of s before you make the assignment. See if it is what you expect.If it is, try to assign a literal string, ie:f.style.fontSize = "10px";That will tell you if something is flawed with the whole process.If all that works, see if IE doesn't like casting the number value. Build the string before you pass it:var sz = s + "px";f.style.fontSize = sz;or even:var sz = String(s) + "px"; Link to comment Share on other sites More sharing options...
suzie Posted April 9, 2010 Author Share Posted April 9, 2010 I did that: h.style.fontSize = s + "px"; alert(s);I got 11only Link to comment Share on other sites More sharing options...
jeffman Posted April 9, 2010 Share Posted April 9, 2010 Great. Most of the function works.Did you try the other suggestions? Link to comment Share on other sites More sharing options...
suzie Posted April 12, 2010 Author Share Posted April 12, 2010 hello, and thanks a lot for your interest and helpyes i tried the second suggestion, but still the errorthe error indicates now to the last bracket "}"I really don't know why, do u think there is a problem in the call of the function???here is the all function:<script language="JavaScript" type="text/javascript"><!-- var min=14;var max=17;function increaseFontSize() { var p = document.getElementsByTagName('h1'); for(i=0; i<p.length; i++) { if(p.style.fontSize) { var s = parseInt(p.style.fontSize.replace("px","")); } else { var s = 15; } if(s!=max) { s += 1; } p.style.fontSize =s + "px"; } var d = document.getElementsByTagName('h2'); for(i=0;i<d.length;i++) { if(d.style.fontSize) { var s = parseInt(d.style.fontSize.replace("px","")); } else { var s = 13; } if(s!=max) { s += 1; } d.style.fontSize =s + "px"; } var e = document.getElementsByTagName('h3'); for(i=0;i<e.length;i++) { if(e.style.fontSize) { var s = parseInt(e.style.fontSize.replace("px","")); } else { var s = 14; } if(s!=max) { s += 1; } e.style.fontSize =s + "px"; } var f = document.getElementsByTagName('h4'); for(i=0;i<f.length;i++) { if(f.style.fontSize) { var s = parseInt(f.style.fontSize.replace("px","")); } else { var s = 12; } if(s!=max) { s += 1; } f.style.fontSize =s + "px"; } var g = document.getElementsByTagName('a'); for(i=0;i<g.length;i++) { if(g.style.fontSize) { var s = parseInt(g.style.fontSize.replace("px","")); } else { var s = 14; } if(s!=15) { s += 1; } g.style.fontSize =s + "px"; } var h = document.getElementsByTagName('span'); for(i=0;i<h.length;i++) { if(h.style.fontSize) { var s = parseInt(h.style.fontSize.replace("px","")); } else { var s = 11; } if(s!=12) { s += 1; } h.style.fontSize =s + "px"; }}function decreaseFontSize() { var p = document.getElementsByTagName('h1'); for(i=0;i<p.length;i++) { if(p.style.fontSize) { var s = parseInt(p.style.fontSize.replace("px","")); } else { var s = 15; } if(s!=15) { s -= 1; } p.style.fontSize =s + "px"; } var d = document.getElementsByTagName('h2'); for(i=0;i<d.length;i++) { if(d.style.fontSize) { var s = parseInt(d.style.fontSize.replace("px","")); } else { var s = 13; } if(s!=13) { s -= 1; } d.style.fontSize =s + "px"; } var e = document.getElementsByTagName('h3'); for(i=0;i<e.length;i++) { if(e.style.fontSize) { var s = parseInt(e.style.fontSize.replace("px","")); } else { var s = 14; } if(s!=min) { s -= 1; } e.style.fontSize =s + "px"; } var f = document.getElementsByTagName('h4'); for(i=0;i<f.length;i++) { if(f.style.fontSize) { var s = parseInt(f.style.fontSize.replace("px","")); } else { var s = 12; } if(s!=12) { s -= 1; } f.style.fontSize =s + "px"; } var g = document.getElementsByTagName('a'); for(i=0;i<g.length;i++) { if(g.style.fontSize) { var s = parseInt(g.style.fontSize.replace("px","")); } else { var s = 14; } if(s!=14) { s -= 1; } g.style.fontSize =s + "px"; } var h = document.getElementsByTagName('span'); for(i=0;i<h.length;i++) { if(h.style.fontSize) { var s = parseInt(h.style.fontSize.replace("px","")); } else { var s = 11; } if(s!=11) { s -= 1; } h.style.fontSize =s + "px"; }}</script> </head><body><div style="height:22px; bottom:0px; direction:rtl"/> <table width="491px" height="22px" border="0" align="center" cellpadding="0" cellspacing="2"/> <tr class="font-author"> <td width="20" bgcolor="#FFFFFF"><a href="java script:decreaseFontSize();" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image19','','images/minus-red.gif',1)"><img src="images/minus.gif" name="Image19" border="0" id="Image19"/></a></td><td width="20" bgcolor="#FFFFFF"><a href="java script:increaseFontSize();" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image19','','images/minus-red.gif',1)"><img src="images/plus.gif" name="Image19" border="0" id="Image19"/></a></td> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.