Forum Moderators: open
Bear in mind, IE uses currentStyle while Mozilla uses getComputedStyle().getPropertyValue()
<html>
<head>
<title>resize test</title>
<script language="javascript">
<!--// date: 6/10/05
// author: James Brennan
var referenceElementID = "text"; // reference element. Must contain some text.
var IE = (navigator.appName == 'Microsoft Internet Explorer')? true : false; //dumb browser check
var referenceElement,storeFontSize;
function init() {
referenceElement = document.getElementById(referenceElementID);
//set initial font size for comparison
storeFontSize = (IE)? referenceElement.currentStyle.fontSize : window.getComputedStyle(referenceElement, null).fontSize ;
//register keystrokes and mouseover events
document.onkeyup = keyCheckFont;
document.onmouseover = windowCheckFont;
}
function keyCheckFont(e) {
// get the key code
var keyCode = e.which;
// these are the hotkeys for changing text size in firefox.
if((keyCode == '61' && e.ctrlKey) ¦¦ (keyCode == '109' && e.ctrlKey)) {
alert('fontSize changed');
}
}
// checks the font size of initElement and compares it to the value lastFontSize.
// if they are different it returns the current font size of initElement.
// If they are the same it returns null.
function fontSizeChanged(storeFontSize,el) {
// get current font size
var curFontSize = (IE)? el.currentStyle.fontSize : getComputedStyle(el, null).fontSize;
// compare it to last font size
if(storeFontSize == curFontSize) return null;
// if they are different return the new value
else return curFontSize;
}
function windowCheckFont(e) {
// compare current font size against last know font size
var tempFontSize = fontSizeChanged(storeFontSize,referenceElement);
// if it hasn't changed do nothing
if(tempFontSize == null) return null;
// if it has, change the known font size to the new size
storeFontSize = tempFontSize;
// and then trigger whatever else you desire
alert('font size changed');
}
-->
</script>
</head>
<body onLoad="init()">
<p id="text">This is some text</p>
</body>
</html>
[edited by: Woz at 12:14 am (utc) on June 24, 2005]
[edit reason] No emails please, see TOS#13 [/edit]
The approach I took was an attempt to keep the script dormant unless there is some reason to believe that the user might have resized the text.
I do not think this could result in any serious memory consuming problem. I do not use timers frequently personally, but there are a lot of scripts which do use more frequent timers : 50 ms, 100ms, etc loops. (vs. once in 1000 ms in this case accordingly to you.)
In any case - every project should be tested before the final verdict ;).
Firefox and Opera, for two examples, use a pixel-based zoom to resize and there are no events built in to those browsers to detect it.
In IE there is no built-in ontextsize or ontextresize event either, but detecting a new selection in IE's Text Size menu can be done.