- Enable information sharing between frames.(connection between frames)
(https://wiki.izihelp.com/vi/knowledgebase/cach-chu-dong-mo-cua-so-chat/)
- Listen for the “message” event and verify if the “topic” property is set to “initialized.”
if (window.addEventListener) { window.addEventListener('message', handleEvents, false); } else if (w.attachEvent) { window.attachEvent('onmessage', handleEvents); } function handleEvents(event) { try { var msg = JSON.parse(event.data); if (msg.topic == "initialized") { <--- kiểm tra topic fillingInfo(event.source); // <--- thực thi } } catch (error) { console.error(error); } }
- Post a message with the topic set to ‘fill-info’ to populate information (after receiving the event with topic = “initialized”).
function fillingInfo(source) { var info = { // [topic] must be "fill-info" topic: 'fill-info', // [data] contains keys, which created when add form setting, and values to be filled data: { name: 'tutorial', email: 'no-email@email.com' } }; source.postMessage(JSON.stringify(info), '*'); }
For example
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0Aif%20(window.addEventListener)%20%7B%0A%20window.addEventListener('message'%2C%20handleEvents%2C%20false)%3B%0A%7D%20else%20if%20(w.attachEvent)%20%7B%0A%20window.attachEvent('onmessage'%2C%20handleEvents)%3B%0A%7D%0A%20%0Afunction%20handleEvents(event)%20%7B%0A%20try%20%7B%0A%20%20%20var%20msg%20%3D%20JSON.parse(event.data)%3B%0A%20%20%20if%20(msg.topic%20%3D%3D%20%22initialized%22)%20%7B%0A%20%20%20%20%20fillingInfo(event.source)%3B%0A%20%20%20%7D%0A%20%7D%20catch%20(error)%20%7B%0A%20%20%20console.error(error)%3B%0A%20%7D%0A%7D%0A%20%0Afunction%20fillingInfo(source)%20%7B%0A%20var%20info%20%3D%20%7B%0A%20%20%20%2F%2F%20%5Btopic%5D%20must%20be%20%22fill-info%22%0A%20%20%20topic%3A%20'fill-info'%2C%0A%20%20%20%2F%2F%20%5Bdata%5D%20contains%20keys%2C%20which%20created%20when%20add%20form%20setting%2C%20and%20values%20to%20be%20filled%0A%20%20%20data%3A%20%7B%0A%20%20%20%20%20name%3A%20'tutorial'%2C%0A%20%20%20%20%20email%3A%20'no-email%40email.com'%0A%20%20%20%7D%0A%20%7D%3B%0A%20source.postMessage(JSON.stringify(info)%2C%20'*')%3B%0A%7D%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />