window.open的小技巧分享
2015-08-16 21:14
483 查看
%26nbsp; %26nbsp; 今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明。
%26nbsp;
%26nbsp; %26nbsp; 聊到window.open,不得不说明一下他的使用方法,主要有两种形式:
%26nbsp;
%26nbsp; %26nbsp; %26nbsp; 通过上面介绍的这种方式可以解决,微博等第三方登录无刷新父页面得知返回状态的需求。同时也是解决跨域请求的一个比较好的方式(安全性不知道如何)。测试小例子 %26nbsp; %26nbsp; %26nbsp; 今天就分享这么一个小技巧吧~
- window.open()没有任何参数,这种方式可以新标签打开页面
- window.open(url, name, pars),带有参数的可以在当前页面打开窗口
- window.open()打开的窗口或者新标签返回窗口的window对象
- 在新窗口里也可以取到父窗口(执行window.open的环境)的window对象,通过window.opner
- window.close()方法可以关闭窗口
- window.onunload和window.onbeforeunload事件,监听窗口关闭情况
%26lt;!DOCTYPE%26gt; %26lt;html%26gt; %26lt;head%26gt; %26lt;meta charset="utf-8"%26gt; %26lt;title%26gt;window.open小技巧%26lt;/title%26gt; %26lt;/head%26gt; %26lt;body%26gt; 我是窗口:window.open %26lt;button id="openWindow"%26gt;点击新开窗口%26lt;/button%26gt; %26lt;script type="text/javascript"%26gt; (function() { var openWindowEl = document.getElementById('openWindow'); var newWindow; var data; window.windowName = 'window.open'; openWindowEl.onclick = function() { newWindow = window.open('window.close.php', '_blank', 'width=500,height=500;'); //需要window.close.html js正常执行之后才行 /*setTimeout(function() { console.log(newWindow.userName); }, 1000);*/ newWindow.onbeforeunload = function() { userName = newWindow.windowName; data = newWindow.data; } newWindow.onunload = function() { alert(userName); if(data.errno == 0) { alert(data.errmsg); }else { alert(data.errmsg); } } }; })(); %26lt;/script%26gt; %26lt;/body%26gt; %26lt;/html%26gt;%26nbsp; %26nbsp; window.close.html页面内容如下: %26nbsp; %26nbsp;%26nbsp;
%26lt;!DOCTYPE%26gt; %26lt;html%26gt; %26lt;head%26gt; %26lt;meta charset="utf-8"%26gt; %26lt;title%26gt;window新开窗口%26lt;/title%26gt; %26lt;/head%26gt; %26lt;body%26gt; 我是窗口:window.close %26lt;script type="text/javascript"%26gt; (function() { window.windowName = 'window.close'; if(Math.random() %26gt; 0.5) { window.data = { errno: 0, errmsg: '成功~' }; }else { window.data = { errno: 1, errmsg: '数据异常~' }; } setTimeout(function() { //拿到的是父窗口的window alert(window.opener.windowName); }, 100); setTimeout(function() { window.close(); }, 1000); })(); %26lt;/script%26gt; %26lt;/body%26gt; %26lt;/html%26gt;
%26nbsp; %26nbsp; %26nbsp; 通过上面介绍的这种方式可以解决,微博等第三方登录无刷新父页面得知返回状态的需求。同时也是解决跨域请求的一个比较好的方式(安全性不知道如何)。测试小例子 %26nbsp; %26nbsp; %26nbsp; 今天就分享这么一个小技巧吧~
相关文章推荐
- window.open的小技巧分享
- window.open的小技巧分享(转)
- 小技巧:window.open弹出新窗口后,如何关闭父窗体
- javascript实现WEB打印(用window.open()实现).不用安装任何控件.(含代码)分享
- javascript新窗口打开链接window.open()被阻拦的解决办法
- 关于ajax请求 window.open被拦截问题
- vwindow.open()详解
- 使window.open() 全兼容IE, Firefox, Chrome的最新写法
- 打开新窗口的window.open使用方法以及最大化等问题解决方法汇总
- ie7中window.open无法调节子窗口大小
- window.open
- window.open
- javascript弹出窗口 window.open使用方法以及参数说明分析篇
- JavaScript-打开新窗口 window.open([URL], [窗口名称], [参数字符串])
- [转]弹出窗口及关闭总结(window.open();Response.Redirect(); 模态) .
- js onclick跳转页面方法(window.open)
- window.open 打开是空白页面
- JS window.open()
- window.open窗口显示在屏幕中央
- Window.open() 全攻略