一.js打開新窗口,經常被攔截
//js打開新窗口,經常被攔截 //指定本窗口打開,可以使用 window.open('http://www.tianma3798.cn', '_self'); //不指定或指定新窗口打開被攔截 window.open('http://www.tianma3798.cn'); window.open('http://www.tianma3798.cn', '_blank');
二、
1.如果是用戶點擊操作,打開新窗口不被攔截
2.如果在ajax回調函數中調用打開新窗口會被攔截
<a href="#" id="a">AAAAA</a> <input type="button" id="btn" value="Open Baidu" onclick="openwin();" /> <script> //如果在ajax回調函數中調用打開新窗口會被攔截 //如果是用戶點擊操作,打開新窗口不被攔截 //可以打開新窗口 document.getElementById('a').onclick = function () { window.open('http://segmentfault.com'); return false; }; //可以打開新窗口 function openwin() { var url = "http://www.baidu.com"; var a = document.createElement("a"); a.setAttribute("href", url); a.setAttribute("target", "_blank"); a.setAttribute("id", "openwin"); document.body.appendChild(a); a.click(); } //其他事件中,觸發打開新窗口 $(window).click(function () { //$('#a').trigger('click'); openwin(); }); </script>
三、Ajax毀掉函數中,打開新窗口解決方案
$(window).click(function () { //Ajax 請求毀掉函數中打開新窗口 var w = window.open(); $.get('../view/test.html', function (data) { w.location.href = '../view/test.html'; }) });