Inaccurate window dimensions on iOS Chrome are causing issues with innerWidth and inner

After rotating my iOS device, Google Chrome version 104 seems to be reporting incorrect values for window.innerWidth and window.innerHeight. The discrepancy is noticeable when comparing the dimensions before and after rotation:

  • Initially in portrait mode: 414 x 714.
  • After rotating to landscape and back to portrait: 390 x 334

To test this issue, I have provided a snippet below. It may require a few attempts before the incorrect values start appearing:

const $ = document.getElementById.bind(document);

const w1 = $("w1");
const h1 = $("h1");
const w2 = $("w2");
const h2 = $("h2");

// Get width/height on page load
w1.innerText = `w: ${window.innerWidth}`;
h1.innerText = `h: ${window.innerHeight}`;

// Get width/height on resize event
function onResizeInstant() {
  w2.innerText = `w: ${window.innerWidth}`;
  h2.innerText = `h: ${window.innerHeight}`;
}

window.addEventListener("resize", onResizeInstant);
body{font-family: sans-serif}
h1{font-size: 16px}
div{
  font-size: 16px;
  height: 20px;
  background: #eee;
  margin-bottom: 2px;
}
<h1>window.innerWidth + Height on initial page load</h1>
<div id="w1"></div>
<div id="h1"></div>

<h1>window.innerWidth + Height after rotating device</h1>
<div id="w2"></div>
<div id="h2"></div>

This issue does not occur with iOS Safari, Chrome Android, Firefox, or any desktop browsers. It appears to be specific to Google Chrome on iOS. If anyone knows what might be causing this behavior or has a workaround for this bug, please share your insights.

Answer №1

It appears that there is a specific issue with Google Chrome on iOS causing a bug. To resolve this, it is recommended to include a brief setTimeout() function before retrieving the values of window.innerWidth and window.innerHeight.

const $ = document.getElementById.bind(document);

const w1 = $("w1");
const h1 = $("h1");
const w2 = $("w2");
const h2 = $("h2");

// Triggered immediately upon resize event
// Might produce incorrect values randomly in Google Chrome on iOS
function onResizeInstant() {
  w1.innerText = `w: ${window.innerWidth}`;
  h1.innerText = `h: ${window.innerHeight}`;
  window.setTimeout(onResizeTimeout, 5);
}

// Executed after a 5ms delay
// Will provide accurate values
function onResizeTimeout() {
  w2.innerText = `w: ${window.innerWidth}`;
  h2.innerText = `h: ${window.innerHeight}`;
}

window.addEventListener("resize", onResizeInstant);

// Call on page load
onResizeInstant();
body{font-family: sans-serif}
h1{font-size: 16px}
div{
  font-size: 16px;
  height: 20px;
  background: #eee;
  margin-bottom: 2px;
}
<h1>window.innerWidth + Height before setTimeout()</h1>
<div id="w1"></div>
<div id="h1"></div>

<h1>window.innerWidth + Height after setTimeout()</h1>
<div id="w2"></div>
<div id="h2"></div>

Outcome:

https://i.sstatic.net/FAS1N.jpg

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choosing an item using the mouse - Three.js

I have a question that I need help with. I'm trying to change the color of an object when it's selected with the mouse, but the code I wrote doesn't seem to be working correctly. Here's what I have so far: <script> v ...

Guide to creating a custom wrapper for a Material UI component with React JS

I am utilizing the Material UI next library for my project and currently working with the List component. Due to the beta version of the library, some parameter names are subject to change. To prevent any future issues, I have decided to create a wrapper a ...

Fixing the Bootstrap Datepicker: A Step-by-Step Guide

Is there a way to configure the Datepicker to display today's date and tomorrow's date? Click here for the JS file Check out the image on our website https://i.stack.imgur.com/VyFUB.jpg ...

Accordion elements that are active will move all other content on the page

I am currently working on creating an accordion using the following code: https://codepen.io/rafaelmollad/pen/JjRZbeW. However, I have encountered a problem where when clicking on one of the accordion items, the content expands and pushes the title upward. ...

Ensuring Angular applications can effectively run on Internet Explorer

In my Angular application, I have implemented the functionality where users can choose a map to select the delivery point for goods. However, there seems to be an issue with this feature in Internet Explorer (IE) - the map opens but the delivery points ar ...

What exactly does the dollar sign signify in plain JavaScript code?

While watching a tutorial on object literals in JavaScript, I noticed something interesting. The instructor demonstrated creating an object like this: var Facebook = { name: 'Facebook', ceo: { firstName: "Mark", favColor: ...

What could be causing this minimal Angular - WebTorrent configuration to fail?

The setup appears to be quite straightforward. Check out the Webtorrent usage reference here This is how I have my setup: import WebTorrent from 'webtorrent'; @Component({ selector: 'app-root', standalone: true, template: `bla` ...

Searching through a json object using typescript

While attempting to retrieve JSON data from a URL, I encountered a particular issue. The problem lies in the fact that each row in the Datarows array contains 5 rows, each row consisting of 47 cells. Out of these 47 cells in each row, I am specifically in ...

The use of jQuery addClass does not produce any changes

I am trying to modify a single attribute in my class using a JavaScript function. .msg_archivedropdown:before { content:""; display: block; position:absolute; width:0; height:0; left:-7px; top:0px; border-top: 10px solid tr ...

Storing a date in MySQL using Vue.js and Node.js

My current tech stack consists of nodejs and express.js for the backend, vuejs for the frontend, and mysql as the database. I am facing an issue where I cannot send a date retrieved from localStorage to my mysql database. Whenever I try to send the date, ...

Using the `map()` method in React's array

I stumbled upon an interesting example at http://codepen.io/lacker/pen/vXpAgj that closely resembles my current issue. Let's consider the following array: [ {category: 'Sporting Goods', price: '$49.99', stocked: true, name: &apo ...

Unable to successfully append a unique character using jQuery

I need help with displaying special characters, specifically the degree symbol (°), in JavaScript/jQuery. Despite my efforts, it is not showing up correctly on the webpage. How can I fix this issue and ensure that the degree sign appears as desired? I ...

Error: ng-messages syntax issue with the field parameter

Encountering the following error: Syntax Error: Token '{' invalid key at column 2 of the expression [{{field}}.$error] starting at [{field}}.$error]. when attempting to execute the code below (form-field.html) <div class='row form-grou ...

How do you modify the up vector of the camera in three.js?

When working with three.js, I encountered a situation where I set the camera's focal point using camera.lookAt(0, 0, 0), but the camera's up vector ended up pointing in a random direction. I have a specific up vector in mind that I want the camer ...

Intermittent loading issues with Highcharts using AJAX requests

We've encountered intermittent errors when using Highcharts. Despite our efforts, we can't seem to pinpoint the cause of these issues in Chrome: Uncaught Highcharts error #16: www.highcharts.com/errors/16 VM210:16 HighCharts was already loaded V ...

Photo captured by camera is not stored in photo gallery

I am currently working on a basic image upload form that allows users to take photos using their phone camera and upload them. However, I have noticed that the pictures taken this way are not being saved to the gallery. Is there something missing in the H ...

Struggling with incorporating a button into a ReactJS table

I am trying to display a (view) button in the table, but I encountered the following error: Module parse failed: Unexpected token < in JSON at position 165 while parsing near '...2021", "view": <button type="submit...' You m ...

Embed a stackoverflow.com iframe within a jsbin

While exploring code using jsbin, I mistakenly linked the iframe to . When my work in loads, it triggers an alert that redirects back to . I am unable to figure out how to modify my jsbin code. Is there a solution or should I start from scratch? ...

Prevent Loading of Nested States in UI-Router

Imagine having two states configured using angularjs ui-router: .state('branding', { url: "/{branding}", controller: 'BrandingCtrl', templateUrl: '/app/Branding.html' }) .state('branding.index', { ur ...

Can a YouTube video be triggered to play when a specific CSS style is selected?

I am searching for a method to display and play different YouTube videos based on a selected CSS style from a drop-down menu. I believe JavaScript can be utilized to detect the chosen CSS. Include the following in the html header of the page. <script ...