Call touch event on parent from iframe

Cameron

I'm trying to call a touch move event when a user interacts with an iframe.

This code is at the top level page which contains the iframe.

var myIframe = document.getElementById('iFrame');
var myIframeDoc = myIframe.contentWindow.document;
myIframeDoc.addEventListener('touchmove', function(event){

    console.log('iframe touched');

    var e = document.createEvent('TouchEvent');
    //e.touches = [{pageX: 10, pageY: 10}];
    //e.initTouchEvent('touchstart', true, true);
    e.initUIEvent('touchstart', true, true);

}, false);

So when the user touch moves on the iframe it will then call one of the events below (also at the top level page).

document.addEventListener("touchmove", function(event){

    alert('touchmove');

});

document.addEventListener("touchstart", function(event){

    alert('touchstart');

});

document.addEventListener("touchend", function(event){

    alert('touchend');

});

However the touch events don't fire on the parent layer... But the console log works fine, so it is picking up the iframe touchmove, but the TouchEvent part doesn't seem to be working.

In short, I need to be able to get the events to fire as though a user has touched the parent page when they interact with the iframe.

Juan Mendes

You are not dispatching the event. I also had to create it as a MouseEvent.

document.addEventListener("touchstart", function (event) {
    alert('touchstart');
});
// Fix 1 (test in mobile, it may need to be feature detected)
// Or not work reliably across browsers, some clients will need TouchEvent
var e = document.createEvent('MouseEvent');
e.initUIEvent('touchstart', true, true);
//Fix 2
document.dispatchEvent(e);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pass an Event to an iframe from the parent window? ( Javascript )

Trigger Custom Event From Iframe To Parent Document

How to dispatch a custom event in Iframe from a parent?

Capture iFrame onhashchange event from the parent

Pass touch event from NestedScrollView to parent view's

Polymer 2.0 call child event from parent

How to prevent an iframe from stealing drop event from parent container?

How to trigger iframe post from parent click event

Sending an authorization token from parent window to iframe origin so that iframe origin could make the call

Iframe inherit from parent

How to call an event in parent component from child component?

Redirect touch event on TextView with ClickableSpan to parent view

Pass UICollectionView touch event to its parent UITableViewCell

Pass touch event from dialog fragment to View right below (inside the parent activity)

how to make GSSendEvent call and simulate a touch event

Stop touch event from being intercepted by ViewPager if there is touch event in webview

Forward a keydown event from the parent window to an iframe that contains reveal.js

Handling iFrame event in the parent web page

select the immediate parent element of an iframe from the iframe

Access parent URL from iframe

how to call a function from iframe

How to handle touch event for child view in parent view

Get innerText from iframe from parent

How to call an event in parent component from child component which is loaded using DCL loadintolocation()

how to call child component's method from parent component's event in Reactjs

How do I call an event handler or method in a child component from a parent?

How to call function in parent class component by onchange event from child component in React?

Call parent form's click event from a child form without any code change in the parent form code winform

Listen to event in VueJS parent component to call a method