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

5

15

Before starting this question, i have to say I really searched everywhere to find the answer but nothing found. Also tried manythings like dispatchevent, postmessage, ... but nothing worked.

Here is the case.

I have a main windows, where I have 4 simple buttons, like downarrow, uparrow, left and right arrow. I want to create a simulation of events pass to the iframe which is in this main window.

In that iframe is a page loaded where is an Eventhandler and react on the arrows.

I tried following but did not worked

var event = document.createEvent('KeyboardEvent'); // create a key event define the event
event.initKeyboardEvent("keypress",       // typeArg,                                                           
true,             // canBubbleArg,                                                        
true,             // cancelableArg,                                                       
null,             // viewArg,  Specifies UIEvent.view. This value may be null.     
false,            // ctrlKeyArg,                                                               
false,            // altKeyArg,                                                        
false,            // shiftKeyArg,                                                      
false,            // metaKeyArg,                                                       
39,               // keyCodeArg (39 is the right arrow key ),                                                      
0);              // charCodeArg);

document.getElementById('vid').dispatchEvent(event);        

Is there anybody who has an idea how I can solve this issue?

Quarterhour answered 23/2, 2015 at 10:57 Comment(4)
Do you control both the JS in the iframe and in the main window? If not, it's just not possible without agreement from both partiesFinally
I control both of themQuarterhour
Are they on the same domain?Finally
Yes, they are on the sameQuarterhour
Q
0

Finally I have sorted out the issue. I have used parent.document on my iframe to catch the events from the parnet side and create them again on iframe and it works great!

Quarterhour answered 27/2, 2015 at 13:39 Comment(1)
Ummm... so, would be nice if you could include some code. Not much of an answer without it. Money, mouth. Please.Adit
K
5

Use postMessage. It works perfectly.

document.getElementById('vid').contentWindow.postMessage(event);
Keiko answered 6/10, 2019 at 7:28 Comment(1)
Does this work without a special reciever in the iFrame document? So just any document unaltered in the iFrame?Adit
U
3

you want something like this:

var iframe = document.getElementById('something');
var iframeEvent = new Event('iframe-keypress');

document.addEventListener('keypress', function (e) {
    iframe.dispatchEvent(iframeEvent);
});

iframe.addEventListener('iframe-keypress', function (e) {
    console.log(e);
});

listen for the event on the document then pass down a custom event to the iframe.

jsfiddle - http://jsfiddle.net/rfkqe64j/

Undamped answered 23/2, 2015 at 11:6 Comment(3)
Unfortunately. this does not pass the event to the iframe. I have set the event listener in the iframe and it does not get any thing:(Quarterhour
Did you click on the fiddle? load the fiddle, focus the output, push some keys, look in the console.Undamped
Yes I did it. but the point is, it does not affect on the iframe. You are changing and passing the event to iframe variable which has no affect on the iframe internally. Try to load a test.html and listen for events on it. then load the test.html into your iframe. You will understand what I exactly mean.Quarterhour
Q
0

Finally I have sorted out the issue. I have used parent.document on my iframe to catch the events from the parnet side and create them again on iframe and it works great!

Quarterhour answered 27/2, 2015 at 13:39 Comment(1)
Ummm... so, would be nice if you could include some code. Not much of an answer without it. Money, mouth. Please.Adit
Q
0

This works, but with jQuery.

window.addEventListener("keydown", (evt) => {
    const {type, key} = evt;
    parent.$("body").trigger(parent.$.Event(type, {key}));
})

Interestingly, parent.$.Event(evt) directly doesn't work.

Quickel answered 17/6, 2019 at 1:52 Comment(0)
P
-3

I think this jquery forum thread should help

https://forum.jquery.com/topic/how-to-pass-mouse-events-to-an-iframe

Publish answered 23/2, 2015 at 11:3 Comment(1)
Please qualify your aegument. Show examples and solid proof. There is no 'should' or 'maybe' in an answer. Show that you have done it, and that it works.Adit

© 2022 - 2024 — McMap. All rights reserved.