The orientation change listener seems to be unresponsive on iOS devices, while it functions properly on Android devices

I encountered an issue with a code snippet I am using:

window.addEventListener("orientationchange", orientation);

Interestingly, the function orientation() is not being triggered on an iPhone SE running iOS 10.3, but it does work on a Moto G running Android 5.1.

The purpose of this code is to update something based on the screen width. To handle browser differences, I utilize window.innerWidth and screen.width if they are undefined.

A glimpse at my orientation() function reveals:

cpr = Math.floor(window.innerWidth/100) - 3;
if (navigator.userAgent.match(/iPhone|iPad|iPod/i) || navigator.userAgent.match(/Android/i)) cpr = Math.min(Math.floor(screen.width/100) - 1, 8);
if (!cpr || cpr <= 0) cpr = 1;
id('times').innerHTML = "";
updateTimes();

The code functions as expected, except for the iOS platform. Some troubleshooting led me to test it in Chrome developer tools' device viewer, where it worked flawlessly.

I sought assistance from various Stack Overflow threads but unfortunately, none provided a solution to my predicament.

To see the code in action, feel free to visit the website link: .

Your insights are greatly appreciated!

Answer №1

I encountered a similar issue with the resize event and was able to pinpoint the cause after coming across an insightful article on this website.

For some time, Mobile Safari had been dispatching the resize event accurately after the viewport was resized and page layout was complete. However, this behavior has changed and now the event is triggered before the page finishes its layout process. This can result in JavaScript issues when trying to access properties like clientWidth, clientHeight, or getBoundingClientRect within an onresize callback.

It's worth noting that the orientationchange event may also be firing prematurely before the page layout is finalized, leading to incorrect values being returned by window.innerWidth.

UPDATE:

Upon further investigation, I discovered that using

document.documentElement.clientWidth
provided the correct value in my case. It might be worth trying out for your situation as well.

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

Papaparse seems to be having trouble parsing all of the data

Currently, I am working on a React application that is responsible for fetching and displaying CSV file data from the public folder. Below is a snippet of my React container: import React, { Component } from 'react' import * as Chart from "char ...

When the HTML content matches a specific value, initiate a click event to trigger

Can anyone help me troubleshoot? I've tried multiple methods but can't seem to get it right. Here's a breakdown of what I'm attempting to accomplish: #info-NUMBER-btn shows Click to display more information. #info-NUMBER CSS is set t ...

What is the best way to loop through every header tag within a designated div container?

Is there a way to cycle through all the header tags within a specific div tag? I am looking to automatically generate a document map tree for everything inside my div tag and display it in a sidebar menu... If there is a user-friendly javascript library av ...

What is the process of calling a function in my WordPress theme's functions.php file from an ajax handler?

I have a function in my theme's functions.php file that I'd like to execute using an ajax handler. An ajax handler is helpful for preventing the need to load the entire WordPress stack (including plugins) for each ajax request. functions.php f ...

Having trouble with an endless loop while utilizing useLazyQuery in React with Apollo and GraphQL?

In the progress of my code, it appears like this: const { ID } = useParams(); const [getObjects, {loading, data}] = useLazyQuery(GET_OBJECTS_BY_ID); const objectWithID = props.data.find(datum => datum._id == ID); if (objectWithID.conditional) { get ...

Personalized HTML Input Field for Digital Keyboard Scores

As a new JavaScript beginner, I have experimented with various approaches in an attempt to create a website that features a digital piano experience without the actual instrument. My goal is to develop a platform where pressing multiple keys together will ...

Tooltip for dynamically fetched data cell within a Bootstrap table

Can anyone provide guidance on how to implement a tooltip for a data cell that receives data from an ajax request? I have not been able to locate any relevant information in the official documentation: I understand how to add tooltips when data is genera ...

I am looking to create a press event using JavaScript libraries like jQuery or Dojo. What is the

When using a touch screen, there is a special trick that can be done to reveal secret windows. If the center of the screen is pressed for 20 seconds, an alert will pop up saying "This is a secret window". Pressing each corner of the touch screen (to ...

What is the best way to conduct end-to-end testing on an Amazon Linux AMI?

Running end to end tests using Protractor has been smooth in Chrome and Firefox on Ubuntu. However, I encountered issues with PhantomJS as it could not locate the elements. My Angular version is v1.2.15. My aim is to test on an Amazon Linux AMI, so either ...

What is the process to set a Woocommerce checkout field as optional when a checkbox is marked?

I need assistance with customizing the checkout page on Wordpress Woocommerce. Specifically, I want to make the field 'billing_name' not required if the checkbox 'buy_on_company' is checked. Currently, I have successfully hidden ' ...

Automatically submitting the form

Is there a way to automatically submit a form once 4 numbers are inputted into the field without having to press enter or click submit? <form action="send.php" method="POST"> <input type="number" maxlength="4"> </form> I have a basic ...

Determine the quantity of classes based on the part name

I have the code snippet provided below: <ul id="towns"> <li class="clearfix odd 516_499_132_0_0 town132">...</li> </ul> I am looking to extract the specific class "town132" as a parameter, but the number - such as 132 in this ...

Struggling to populate a nested array in a dropdown using AngularJS

I am currently working on populating JSON data into a dropdown list, specifically focusing on main category, sub category and sub sub category. Successfully managed to populate the main category and sub category, but facing challenges with populating subsu ...

What is the best way to incorporate and execute a separate JavaScript file in a Node application using Express JS?

I am attempting to configure a javascript file that is responsible for grabbing an RSS feed and storing the information in a database. Originally, I had this file called by the HTML page, but now I want it to continuously run in the back-end (constantly re ...

Node.js throws an error when accessing req.body as undefined

My task involved creating a basic API using node.js and express, with the addition of body-parser. However, I encountered an issue where req.body was returning undefined. Here is a snippet of my app.js: const express = require('express'); const b ...

What could be causing this Ajax error to occur when attempting to post HTML using stringify?

When attempting to post HTML in a string using stringify, an Ajax error occurs. Why is this happening? It appears that the characters are being automatically escaped by stringify. Do I need to manually escape them? Thank you for any guidance. var s; // ...

How can one effectively terminate a node.js route?

I am currently working on a node.js / express route that checks for the presence of a cookie before proceeding. The current implementation is functioning correctly, but now I want to halt the execution of the route if the required cookie is not set. Here ...

NextJS does not automatically define environment variables

My current challenge involves working with the next-auth library, which relies on using environment variables in a specific way: Providers.GitHub({ clientId: process.env.GITHUB_ID, clientSecret: process.env.GITHUB_SECRET, }), Despite following ...

The Node.js application encounters a blank response when making a GET request to the API

As a newcomer to node.js, I'm attempting to describe the issue at hand as clearly as possible. If clarification is needed, please let me know. In my current node.js project, I am faced with a challenge where I need to take a code received from the re ...

The functionality of Jquery $(window).load is not behaving as anticipated when used within a script that is referenced from within an

I am facing an issue with loading animations into an iframe on my webpage. Each content piece is an html file with references to javascript/jquery scripts for animations. content.html <head> <link rel="stylesheet" type="text/css" href="css/slide ...