最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - can we pass DOM elements using postMessage? - Stack Overflow

programmeradmin0浏览0评论

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 ?

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 ?

Share Improve this question edited Sep 18, 2013 at 16:43 Jags asked Sep 18, 2013 at 15:08 JagsJags 1,4661 gold badge18 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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.

You might try with innerHTML, which "gets or sets the HTML or XML markup contained within the element." (https://developer.mozilla/en-US/docs/Web/API/Element/innerHTML)

With innerHTML you can pass element's content as a string, and then, in the other iframe, insert that content, again with innerHTML.

In this answer there is a simple way to get the innerHTML content of a node: https://stackoverflow./a/1750860/1401341

You won't get events attached to the element, but in some cases this method could be enough.

发布评论

评论列表(0)

  1. 暂无评论