跨域

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>window.name 解决跨域</title></head><body><div id="lxj" style="font-size:14px;line-height:32px;padding:5px 0 5px 5px"><img src="loading.gif" alt="" style="vertical-align:middle;margin-right:5px" />正在获取数据</div><script type="text/javascript">


var iframe = document.createElement("iframe");
	iframe.style.display = "none";
	document.body.appendChild(iframe);
	(function () {
	    var same_domain = false;
	    // 当iframe加载完之后触发的函数
	    function iframe_load() {
	        if (same_domain) {
	            // 取得从服务器返回的数据
	            document.getElementById('lxj').innerHTML=iframe.contentWindow.name;
	            // 关闭iframe的窗口
	            iframe.contentWindow.close();
	            // 移除iframe
	            document.body.removeChild(iframe);
	        } else {
	            same_domain = true;
	            // 不能用iframe.src = "proxyUrl.html",在IE下有错误
	            iframe.contentWindow.location = "proxyUrl.html";
	        }
	    }
		
		
	    // 在IE下要用attachEvent来添加iframe的onload处理函数
	    if (iframe.attachEvent) {
	        iframe.attachEvent("onload", function () {
	            iframe_load();
	        });
	    }
	    else {
	        iframe.onload = iframe_load;
	    }
		
		
	})();
	iframe.src = "http://louxiaojian.googlecode.com/svn/trunk/notes/javascript/iframe-crossdomain-communication/window_name/b.html";

</script></body></html>
posted @ 2013-01-23 14:28  {前端开发}  阅读(201)  评论(0编辑  收藏  举报