Search code examples
javascriptiosmobilewindowviewport

JS - Safari on iOS - How to get viewport scale property


On Chrome, I can get the viewport scale by looking at window.visualViewport.scale it is available on Safari, however, it seems that window.visualViewport is not defined on older versions of Safari on iOS.

Is there any workaround?


Solution

  • Firstly, you need to get viewport width by using clientWidth or innerWidth

    var viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
    

    Then, you need to get actual screen width

    var screenWidth = window.screen.width
    

    Then, viewport scale is on your hand

    var viewportScale = screenWidth / viewportWidth