iOS is currently experiencing issues with touch events not functioning properly within iFrames

Events like touchstart or touchend do not work when attached to the window inside an IFrame on iOS devices.

Check out this straightforward example:

mainpage.html

<!DOCTYPE HTML>
<html style="height: 100%;">
<head>
    <meta charset="UTF-8">
    <title>Touch Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="height: 100%; margin: 0; overflow: hidden;">
    <iframe style="width: 100%; height: 100%; border: none;" src="subpage.html"></iframe>
</body>
</html>

subpage.html

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Touch Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>    
        body {
            margin: 0;
            padding: 8px;
        }

        div.header {
            margin-bottom: 8px;
        }

        div.text-entry {
            font: 300 1rem/1.25 'Open Sans', 'Helvetica Neue', helvetica, arial, sans-serif;
            color: rgb(64, 64, 64);
        }
    </style>

    <script>
        window.onload = function() {
            function addEvent(event) {
                var div = document.createElement('div');

                div.className = 'text-entry';
                div.textContent = new Date().toLocaleTimeString() + ' Event "' + event.type + '" detected';
                document.body.appendChild(div);
            }

            window.addEventListener('touchstart', addEvent.bind(null), false);
            window.addEventListener('touchend',   addEvent.bind(null), false);
        }
    </script>
</head>
<body>
    <div class="text-entry header">Try clicking/touching the viewport to see text entries added...</div>
</body>
</html>

Many have faced scroll and event issues on iOS in IFrames, but solutions seem elusive for the problem at hand.

I've set up a CodePen and a JSFiddle for experimentation, both demonstrating the same behavior within an IFrame.

Answer №1

Resolution 1: To resolve touch listener issues on Safari for IOS within an iframe, simply add a dummy listener to the document object:

document.addEventListener('touchstart', {}); // in iframe

Safari seems to restrict touch listeners on window when other DOM objects lack listeners.

Resolution 2: In the main window, add a dummy listener to any object (such as window):

window.addEventListener('touchstart', {}); // in top window

Safari on IOS may block touch listeners to window in iframes unless the parent also has listeners.

Either of the above fixes should suffice (only one is necessary, not both). Tested on:

  • Safari 9.0 / IOS: 9.3.5
  • Safari 11.0 / IOS: 11.3

Answer №2

To resolve the problem I was facing, I implemented the below CSS into the content of the IFrame (child.html in the example provided above):

html, body {
    touch-action: auto;
}

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

What is the best way to add a button click event listener that persists through DOM changes, such as in a single-page application?

I have developed a ViolentMonkey userscript that adds an event listener to a button with the ID #mark-watched. When this button is clicked, it automatically triggers a click on the button with the ID #next-video. This functionality is necessary because the ...

Adjusting the border-right size based on scroll position

Apologies if this question seems trivial, but I am completely new to coding in HTML and JavaScript. I understand that I can make some changes based on scroll position, but right now I'm a little confused. I want to increase the height of a box as the ...

Utilizing a single delete function for a post request in jQuery or a PHP request

Seeking guidance on how to achieve a specific task. I have a controller that loads a view containing a table listing pages from my database. Each row in the table has an icon that, when clicked, performs one of two actions. If the user does not have javas ...

What is the best way to display the current time (generated through JavaScript) within a designated div element?

During my JavaScript studies, I stumbled upon the following code snippet: function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.body.innerHTML = hours+":"+mins+":"+sec ...

Angular alert: The configuration object used to initialize Webpack does not conform to the API schema

Recently encountered an error with angular 7 that started popping up today. Unsure of what's causing it. Attempted to update, remove, and reinstall all packages but still unable to resolve the issue. Error: Invalid configuration object. Webpack ini ...

What is the best way to add a key to a JavaScript array while keeping it reactive in Vue.js?

If there's an array in the state: state: { users: [] }, Containing objects like: { id: 1, name: "some cool name" } To add them to the store using a mutator like users.push(user);, how can we ensure that instead of 0:{...}, it uses the ...

Having trouble binding form data to a React component with the onChange() method?

I've been working on developing an email platform exclusively for myself and encountered a roadblock with this React form not updating state when data is entered. After identifying the issue, it appears that the main problem lies in the React form not ...

Revising variables in Java Script

How can I shuffle a list of variables in JS and maintain the order while changing their values? The following code snippet demonstrates what I am trying to achieve. <p id="demo"></p> <script> var gen = "male "; var race = "white"; var ...

Having trouble generating an array for checkboxes using jQuery, AJAX, and PHP

I'm almost there, but there's something missing. I'm attempting to send variables from multiple checkboxes to a PHP page using jQuery. This is my code: <div id="students"> <div class="field"> <label> ...

``Why is my setFeatureState function not updating the value in my Mapbox map

I've been attempting to change the stroke of a circle upon clicking it on mapbox. Despite following mapbox's documentation, the values don't seem to update. The console is also clear. t.map.addLayer({ id: id, type: 'circle&apo ...

how to change the color of a specific item in a list created using v-for

I'm a beginner when it comes to vueJs and I'm attempting to toggle the class "active" on a single element once it has been clicked. Currently, my code toggles all elements with the class material_icons. How can I modify it to toggle only the elem ...

The framework of AngularJS modules

As I delve into structuring multiple modules for our company's application, I find myself at a crossroads trying to design the most optimal architecture. Research suggests two prevalent approaches - one module per page or a holistic 'master' ...

Unable to view text on both IOS and Android platforms when using React Native

So I was working on developing my app and encountered an issue with displaying text on the HomeScreen. Strangely, the text appears fine on the web version but is not showing up on both Android and IOS platforms. Below is the snippet of code that I am using ...

What makes Twitter Bootstrap special that modal events function in JQuery but not in pure JavaScript?

When working with the Twitter Bootstrap modal dialog, there is a set of events that can be utilized with callbacks. For instance, in jQuery: $(modalID).on('hidden.bs.modal', function (e) { console.log("hidden.bs.modal"); }); I ...

Is there a way to render a component without having to render AppComponent constantly?

I am looking to display two components (AppComponent and UserComponent) without constantly displaying AppComponent. Here's how my code is structured: app.routing.module.ts const routes: Routes = [ { path: '', component: AppComponent ...

Configuring JSON in PHP and JavaScript

Although I have already asked this question before, I am experiencing some issues in my code. I know that utilizing JSON is necessary and after researching multiple sources, I grasp the concept but somehow am unable to implement it correctly. Here is my co ...

Integrate ng-admin with ui-router for enhanced functionality

My AngularJS project (version 1.4.9) is built using ui-router and contains multiple states defined as follows: .state('overview', { url: '/overview', parent: 'dashboard', templateUrl: 'views/dashboard/overview.html ...

Initial input firing empty string on React's onChange event

Issue - When I input text for the first time, no string output is displayed. As a result, the last character of the input is not showing up. What could be causing this? Specific focus - Concentrating on the name property const [options, setOptions] = useS ...

Is it necessary to use JS/JQ to trigger PHP form data?

Can PHP files/functions be executed without reloading the page? It can be quite disruptive when developing a chat app and every time you send a message, the entire page refreshes. I attempted to use AJAX but it didn't work. Is it not possible to send ...

Unexpected behavior observed: Title attribute malfunctioning upon double clicking span element

I recently implemented the following code: <span title="hello">Hello</span> Under normal circumstances, the title attribute functions correctly when hovering over the element. However, after double clicking on the span element (causing the t ...