Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we pass DOM elements using postMessage?

I have 2 iframes on a page and I am trying to pass DOM element from one iframe to another using postMessage. But chrome keep giving me error:

"DataCloneError: An object could not be cloned. "

Is there a way around this issue ?

like image 535
Jags Avatar asked Sep 18 '13 15:09

Jags


People also ask

How do you send objects in postMessage?

Add a textarea and a send button that will be used to send messages to child window. Send the text of textarea using . postMessage(message, targetOrigin) when the button is clicked. In order send and receive JSON objects instead of a simple string, JSON.

What does window postMessage do?

postMessage() The window. postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.

How do you grab elements from DOM?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object.

Is postMessage safe?

postMessage is generally considered very secure as long as the programmer is careful to check the origin and source of an arriving message. Acting on a message without verifying its source opens a vector for cross-site scripting attacks.


1 Answers

According to the spec, you can't send DOM nodes and it will throw an error.

Messages can be structured objects, e.g. nested objects and arrays, can contain JavaScript values (strings, numbers, Date objects, etc), and can contain certain data objects such as File Blob, FileList, and ArrayBuffer objects.

like image 196
Haohan Avatar answered Oct 20 '22 03:10

Haohan