Javascript compatibility issues (code works in FF

2019-08-13 22:08发布

问题:

I have a code in Javascript as follows...

var btnOK = document.getElementById('btnOK');
btnOK.style.visibility = "visible";
var iframeControlObj = document.getElementById('iframe');
this.style.visibility = "hidden";
var file_xml = iframeControlObj.contentDocument.getElementById('hiddenxml');
file_xml.value = xml_value;
iframeControlObj.contentWindow.location.reload();

Explanation

I have an iframe in which there is a control "hiddenxml". I want to send the data into the iframe from the current webpage. So, I am initializing the value to the "hiddenxml" and reloading the iframe.

Problem

In Firefox, I am able to get "xml_value" through the "hiddenxml", but in Chrome and Opera, I am unable to get this information it.

Please let me know why this is happening. If this is a trash method, please feel free to give a better method and improve it. Thanks a lot..

PS: The xml_value can be quite large, about 3-4 MB in size. xml_value is a string and not a DOM object

回答1:

If you want to exchange data between your page and an iframe you can use

window.postMessage

The Mozilla Documentation about this is quite good: https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage

Maybe this will help you.