0

I want to trigger the ctrl+0 in order to reset the browser zoom. If I do it manually and press ctrl+0 then it resets the browser zoom.

Here is the code that I've tried:

$('#test').click(function() {
         e = $.Event("keypress");
         e.which = 48;
         e.ctrlKey = true;
         $(window).trigger(e);

        });  

but it doesn't work. I tried to trigger the event on $(document) as-well but still no success.

Any idea how can I do that?

kfirba
  • 5,231
  • 14
  • 41
  • 70

3 Answers3

1

It doesn't work because the ctrl+0 key combination is interpreted by the browser first and than sent to the event loop. If you trigger it programmatic, it will go directly to the event loop.

In css there is a zoom property.

What you could probably do is:

  • use a method described here, to detect the browser zoom level
  • set the zoom property to compensate for the browser zoom.

This way if the user has zoom 120%, you set from css zoom 80%.


Anyway, I advice against it because 99% of the time, the user wants/needs to zoom.

What it is better, would be to make your website look good even when zoomed, by using em units, percent values and maybe media-queryes

Community
  • 1
  • 1
Andrei
  • 3,086
  • 2
  • 19
  • 24
  • Hey! I couldn't use the script the guy posted there since it returned with many errors. to be honest, I don't really care what is the user current zoom level, I would like to reset it to be 100%, isn't there any way to do that? – kfirba Apr 18 '14 at 09:15
  • No. The browser zoom comes before the css `zoom` property. Because of that you have no control over it. Strange that nothing from that entire answer worked for you. – Andrei Apr 18 '14 at 09:17
-2
$(document).bind('keydown', function (e) {
    if (e.ctrlKey && e.which == 48) {
        // your code here..
    }
});

// if you wanna to resize (zoom in/out window)?
// your code goes here
$(window).resizeTo(w, H);

// if you wanna to set zoom attr?
// your code goes here
$('whatever').css('zoom', value);
TechStone
  • 141
  • 6
-2

Trust me, the best method is to use task scheduler to trigger a .vbs mentioned in http://www.computerhope.com/forum/index.php?topic=76100.0

To reset zoom in browser, use the following vbs

Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "^0"
NewComer
  • 307
  • 1
  • 3
  • 8