双面玻璃窗:Twoface Window
- 无辣不欢 - 爱…稀奇~{新鲜:科技:创意:有趣}来自设计师Junkyung Kim等人的创意,双面玻璃窗(Twoface Window),让玻璃窗绕着窗户的中轴转动开合,于是,当户外天气恶劣时,我们可以将窗户上的花盆转向室内,再不用临时搬来搬去那么麻烦了. 嗯,对园艺爱好者来说,这会是个不错的解决方案. 设计师:Junkyung Kim、Yonggu Do.
有三个页面:
实现起来基本步骤如下:
<script type="text/javascript">
window.name = 'I was there!'; // 这里是要传输的数据,大小一般为2M,IE和firefox下可以大至32M左右
// 数据格式可以自定义,如json、字符串
</script>
<script type="text/javascript">
var state = 0,
iframe = document.createElement('iframe'),
loadfn = function() {
if (state === 1) {
var data = iframe.contentWindow.name; // 读取数据
alert(data); //弹出'I was there!'
} else if (state === 0) {
state = 1;
iframe.contentWindow.location = "http://a.com/proxy.html"; // 设置的代理文件
}
};
iframe.src = 'http://b.com/data.html';
if (iframe.attachEvent) {
iframe.attachEvent('onload', loadfn);
} else {
iframe.onload = loadfn;
}
document.body.appendChild(iframe);
</script>
<script type="text/javascript">
iframe.contentWindow.document.write('');
iframe.contentWindow.close();
document.body.removeChild(iframe);
</script>
总结起来即:iframe的src属性由外域转向本地域,跨域数据即由iframe的window.name从外域传递到本地域。这个就巧妙地绕过了浏览器的跨域访问限制,但同时它又是安全操作。