I need to emulate a key press with click on a link. The keyboard shortcut CTRL++ must be called with a click on a link, or a similar function.
Asked
Active
Viewed 1,509 times
2
-
are you trying to increase/decrease font size when a link is clicked? – charliegriefer Dec 08 '10 at 11:25
2 Answers
3
You can't invoke key press with JavaScript.
However, you can control zoom level with JavaScript, here is example for IE and Chrome: http://jsfiddle.net/cMCuz/2/
Edit: now supports Firefox as well.
Required JavaScript:
var _zoomLevel = 100;
function ChangeZoomLevel(diff) {
_zoomLevel += diff;
var oDiv = document.getElementById("Contents");
if (typeof oDiv.style.MozTransform == "string")
oDiv.style.MozTransform = "scale(" + (_zoomLevel / 100) + ")";
else if (typeof oDiv.style.zoom == "string")
oDiv.style.zoom = _zoomLevel + "%";
}
Sample buttons:
<button type="button" onclick="ChangeZoomLevel(10);">+</button>
<button type="button" onclick="ChangeZoomLevel(-10);">-</button>
This will change the zoom level of whole DIV element, can be changed easily to change zoom level of whole document if required.

Shadow The GPT Wizard
- 66,030
- 26
- 140
- 208
-
-
Poor Opera, I think you might have hurt its feelings - I can see it crying in the corner ;-) – Andy E Dec 08 '10 at 12:12
-
@Andy - well, can't enjoy *all* worlds... supporting the three major browsers is my current goal in life. :D – Shadow The GPT Wizard Dec 08 '10 at 12:15
-
Hi i tried your solution it seems to be working for every browser but not working in IE 7,8 , Does there is any sols for this , Please its urgent – rohitpurohit Dec 08 '10 at 12:37
-
@user329539 sorry, not going to install Opera now - supporting the three major browsers is the most I can do ATM. It's possible to hide the buttons if the browser is not one of these to avoid confusion. – Shadow The GPT Wizard Dec 21 '10 at 15:03
0
can i contribute please. adding on Shadow Wizard code:
var _zoomLevel = 100;
function ChangeZoomLevel(diff) {
_zoomLevel += diff;
var oDiv = document.getElementById("Contents");
if (typeof oDiv.style.MozTransform == "string")
oDiv.style.MozTransform = "scale(" + (_zoomLevel / 100) + ")";
else if (typeof oDiv.style.zoom == "string")
oDiv.style.zoom = _zoomLevel + "%";
else if (typeof oDiv.style.OTransform == "string")
oDiv.style.OTransform = "scale(" + (_zoomLevel / 100) + ")";
else if (typeof oDiv.style.WebkitTransform == "string")
oDiv.style.WebkitTransform = "scale(" + (_zoomLevel / 100) + ")";
else if (typeof oDiv.style.MsTransform == "string")
oDiv.style.MsTransform = "scale(" + (_zoomLevel / 100) + ")";
else if (typeof oDiv.style.transform == "string")
oDiv.style.transform = "scale(" + (_zoomLevel / 100) + ")";
}
i think it works in all browsers now.

alhoseany
- 761
- 9
- 28