<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[使window.open() 全兼容IE, Firefox, Chrome的最新写法]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 28 May 2014 08:45:11 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：新窗口打开下载页时出现Firefox第一次会提示拦截。<br/><br/>window.open() 可谓是 弹窗广告的利器, 不过因为浏览器的拦截机制越来越完善, 打开几率也越来越低了.<br/>之前在百度上看到很多种写法,如: <br/>&nbsp;&nbsp;&nbsp;&nbsp;通过 js 去触发某按钮的click事件<br/>&nbsp;&nbsp;&nbsp;&nbsp;通过 js 去触发某form的submit事件,并且form的target 设置为_blank<br/>&nbsp;&nbsp;&nbsp;&nbsp;不过经过我的测试，发现都已经不再兼容了于是想到了下面这个办法：<br/><br/><br/><textarea name="code" class="php" rows="15" cols="100">
&lt;script&gt;&nbsp;&nbsp;&nbsp;&nbsp;
var new_window = null;&nbsp;&nbsp;

//加一个确认,可以在这个方法里面执行很多适用于自己的判断...
function sure_open() &#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;if (confirm(&#039;确定要打开php爱好者吗？&#039;))&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open_window(url);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_window.close(); //关闭窗口
&#125;&nbsp;&nbsp;

function open_window(url) &#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;new_window.location.href = url;&nbsp;&nbsp;&nbsp;&nbsp;
&#125;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/script&gt;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;input type=&quot;button&quot; onclick=&quot;new_window = window.open(); open_window(&#039;http://www.phplover.cn&#039;)&quot; value=&quot;打开php爱好者&quot; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;
</textarea><br/>加入关闭功能：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;script&gt;&nbsp;&nbsp;&nbsp;&nbsp;
var new_window = null;&nbsp;&nbsp;

//加一个确认,可以在这个方法里面执行很多适用于自己的判断...
function sure_open() &#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;if (confirm(&#039;确定要打开php爱好者吗？&#039;))&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open_window(url);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_window.close(); //关闭窗口
&#125;&nbsp;&nbsp;

function open_window(url) &#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;new_window.location.href = url;&nbsp;&nbsp;&nbsp;&nbsp;
&#125;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/script&gt;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;input type=&quot;button&quot; onclick=&quot;new_window = window.open(); open_window(&#039;http://www.jackxiang.com&#039;)&quot; value=&quot;打开jackxiang.com&quot; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;input type=&quot;button&quot; onclick=&quot;new_window.close();&quot; value=&quot;关闭打开的jackxiang.com&quot; /&gt;&nbsp;&nbsp;
</textarea><br/>其中用到的原理就是<br/> 通过用户真实的点击 触发window.open() 打开一个新窗口(因为是真实的用户行为，浏览器会认为是用户意愿，不会进行拦截)<br/>然后再通过js去更改新窗口的 location<br/><br/>来自：http://www.phplover.cn/post/480.html<br/><br/><br/>原理：<br/>window.open是javascript函数，该函数的作用是打开一个新窗口或这改变原来的窗口，不过一般用来的是打开新窗口，因为修改原来的网页地址，可以有另一个函数，那就是window.location,他可以重定向网页地址，使网页跳转到另一个页面。<br/><br/>我现在要说的是window.open函数的几个使用策略，一般情况下，如果你直接在js中调用window.open()函数去打开一个新窗口，浏览器会拦截你，认为你将弹出广告等用户不想得到的窗体，所以如果不想让浏览器拦截你，你可以将这个函数改为用户点击时触发，这样浏览器就认为是用户想访问这个页面，而不是你直接弹出给用户。<br/><br/>所以常用的方法就是在超链接里加入onclick事件，如&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;window.open()&quot;&gt;&lt;/a&gt;这样用户点击这个超链接，浏览器会认为它是打开一个新的链接，所以就不会拦截。<br/><br/>可是有时候我们会遇到想要弹出一个窗口，可是却是在onckick事件执行后，才去弹出来的，这时就会被浏览器拦截，我们可以通过下面的方法来避免，就是先用window.open打开一个窗口，然后修改地址。如var tempwindow=window.open(&#039;_blank&#039;);打开一个窗口，然后用tempwindow.location=&#039;http://www.baidu.com&#039;;使这个窗口跳转到百度，这样就会呈现弹出百度窗口的效果了。<br/><br/>方法二：<br/>由于在使用window.open时，在很多情况下，弹出的窗口会被浏览器阻止，但若是使用a链接target=&#039;_blank&#039;，则不会，基于这一特点，自己封装了一个open方法：<br/><textarea name="code" class="php" rows="15" cols="100">
function openwin(url) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;var a = document.createElement(&quot;a&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;a.setAttribute(&quot;href&quot;, url);
&nbsp;&nbsp;&nbsp;&nbsp;a.setAttribute(&quot;target&quot;, &quot;_blank&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;a.setAttribute(&quot;id&quot;, &quot;camnpr&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;document.body.appendChild(a);
&nbsp;&nbsp;&nbsp;&nbsp;a.click();
&#125;
</textarea><br/>实践OK代码如下所示：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;script&gt;&nbsp;&nbsp;&nbsp;&nbsp;
function openwin(url) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;var a = document.createElement(&quot;a&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;a.setAttribute(&quot;href&quot;, url);
&nbsp;&nbsp;&nbsp;&nbsp;a.setAttribute(&quot;target&quot;, &quot;_blank&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;a.setAttribute(&quot;id&quot;, &quot;camnpr&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;document.body.appendChild(a);
&nbsp;&nbsp;&nbsp;&nbsp;a.click();
&#125;
&lt;/script&gt;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;body&gt;
&lt;input type=&quot;button&quot; id=&quot;btn&quot; value=&quot;向东博客&quot; onclick=&quot;openwin(&#039;http://jackxiang.com&#039;);&quot; /&gt;
&lt;/body&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
</textarea><br/>调用方式如下：<br/>&lt;input type=&quot;button&quot; id=&quot;btn&quot; value=&quot;向东博客&quot; onclick=&quot;openwin(&#039;http://jackxiang.com&#039;);&quot; /&gt;
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 使window.open() 全兼容IE, Firefox, Chrome的最新写法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>