尝试用iframe解决跨域问题.上传附件到其它服务器器,获取返回数据,数据存在iframe的window.name中,在主页面获取iframe 里的变量。如何获取?

解决方案 »

  1.   

    document.frames["对应name"].document.getElementById("ID").value
      

  2.   

    <html>
    <body>
    <input type="button" id="button_how" value="看看现在几点了?"/>
    <script>
    function getWindowName(url, callback) {
    if (!url || !callback) return;
    var frame = document.createElement("iframe"), state = 0;
    frame.style.display = "none";
    document.body.appendChild(frame);
    frame.onload = frame.onreadystatechange = function() {
    if (this.readyState && this.readyState != "complete") return;
    if (state) {
    var name = frame.contentWindow.name;
    frame.parentNode.removeChild(frame);
    callback(name);
    } else {
    state = 1;
    frame.contentWindow.location = "about:blank";
    }
    };
    frame.src = url;
    }
    (function() {
    document.getElementById("button_how").onclick = function() {
    getWindowName("http://www.renrousousuo.com/tools/cross.aspx?" + (+new Date()).toString(36), function(name) {
    alert(name);
    });
    };
    })();
    </script>
    </body>
    </html>