Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 829
Moha Avatar asked Feb 23 '15 10:02

Moha


People also ask

How do I pass an iframe event?

In that iframe is a page loaded where is an Eventhandler and react on the arrows. var event = document. createEvent('KeyboardEvent'); // create a key event define the event event. initKeyboardEvent("keypress", // typeArg, true, // canBubbleArg, true, // cancelableArg, null, // viewArg, Specifies UIEvent.

Can parents communicate with iframe?

All you have to do is first dispatch an event from the iframe to the parent that notifies the parent that the iframe is loaded (essentially a "ready message"). The parent will be listening for messages and if it receives the "ready message" event, it can then reply to the iframe with whatever message you want to send.

Can iframe access parent windows?

The window will get opened by using the iframe, and the communication could be done with the parent window from the child window. To call a parent window function, use the following syntax and also refer to the code given below.

How does iframe pass value to parent?

Sending some data from the child iframe to the parent window is also pretty simple. Whenever you embed an iframe, the iframe will have a reference to the parent window. You just need to use the PostMessage API to send data via the window. parent reference of the parent window.


1 Answers

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!

like image 134
Moha Avatar answered Oct 25 '22 14:10

Moha