Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - prevent page zooming

I have a web application that will be used on a tablet PC (on Internet Explorer). EDIT : not a tablet, but a Windows 7 computer with a touch screen.

The problem is that the user can pinch to zoom on the page (like ctrl-+).

Is there a way to disable it from JavaScript ? (like on mobile devices).

Or maybe change the User Agent to act like an iPad for example ? Will it work ?

like image 335
Gab Avatar asked Jul 18 '14 13:07

Gab


People also ask

How do I stop my page from zooming in?

To disable the zooming option with the multi-touch gesture we can use surefox browser but still, a user can zoom in or out by double tapping on the screen. We can use the <meta> tag to disable zoom in and out on a mobile web page.

How do I turn off HTML scaling?

The viewport <meta> tag prevents the user from scaling This means that the URL in question contains a viewport <meta> tag with the attribute user-scalable set to '0' or 'no', which prevents the user from zooming in or zooming out.

Can JavaScript detect the browser zoom level?

Method 1: Using outerWidth and innerWidth Property: It is easier to detect the zoom level in webkit browsers like Chrome and Microsoft Edge. This method uses the outerWidth and innerWidthproperties, which are the inbuilt function of JavaScript.


2 Answers

You can disable zoom in browser by Ctrl+ or Ctrl- or Using Ctrl Key + Mouse wheel Up or down by this code.

$(document).keydown(function(event) {
if (event.ctrlKey==true && (event.which == '61' || event.which == '107' || event.which == '173' || event.which == '109'  || event.which == '187'  || event.which == '189'  ) ) {
        event.preventDefault();
     }
    // 107 Num Key  +
    // 109 Num Key  -
    // 173 Min Key  hyphen/underscor Hey
    // 61 Plus key  +/= key
});

$(window).bind('mousewheel DOMMouseScroll', function (event) {
       if (event.ctrlKey == true) {
       event.preventDefault();
       }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
teste
like image 21
Felipe Guimaraes Avatar answered Sep 20 '22 13:09

Felipe Guimaraes


Use

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

in the <head> section of your document to prevent scaling the website on mobile devices. Important here is user-scalable=no which makes the trick.

Edit

Made some more research and you also got the option to add:

<meta name="MobileOptimized" content="640">

While content="640" is the width you want to set and behaves like <meta name="viewport" content="width=640,user-scalable=no">.

Read more about here and here.

like image 65
morkro Avatar answered Sep 18 '22 13:09

morkro