我编写代码在新窗口中打开弹出窗口。我打开这个窗口几秒钟之后会自动关闭。我想要的是有人在那段时间之前关闭它。我会发现它并向他显示信息。这是我正在使用的代码
$(document).ready(function() {
var myWindow;
$("#idview").click(function() {
var vidurl = $('#vurl').val();
counter();
myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");
});
function counter() {
var n = $('.c').attr('id');
var c = n;
$('.c').text(c);
setInterval(function() {
c++;
if (c <= 41) {
$('.c').text(c);
}
if (c == 41) {
$('.c').text(n);
}
}, 1000);
}
setInterval(function() {
myWindow.close();
}, 45000);
window.onbeforeunload = closingCode;
function closingCode(){
alert('hitme');
return null;
}
});
我尝试使用window.ununload
,但它不起作用。任何人都可以告诉我如果有人要关闭浏览器弹出窗口怎么办?
由于
答案 0 :(得分:2)
您可以像这样使用onbeforeunload:
window.onbeforeunload = function (e) {
var e = e || window.event;
};
更多参考: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload
答案 1 :(得分:1)
1st:您需要将onbeforeunload
事件绑定到AFTER
opened
窗口。
第二名,请将onbeforeunload
事件代码移至click event
函数
第3名: Window.onbeforeunload
更改为myWindow.onbeforeunload
var myWindow;
$("#idview").click(function() {
//var vidurl = $('#vurl').val();
var vidurl = "google.com";
counter();
myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");
myWindow.onbeforeunload = function closingCode(){
alert('hitme');
return null;
}
});
答案 2 :(得分:0)
您可以使用解决方案https://jsfiddle.net/rs01x0oy/
var myWindow;
$("#idview").click(function() {
var vidurl = $('#vurl').val();
counter();
myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");
myWindow.onbeforeunload = function(){
console.log("Closed");
};
});
function counter() {
var n = $('.c').attr('id');
var c = n;
$('.c').text(c);
setInterval(function() {
c++;
if (c <= 41) {
$('.c').text(c);
}
if (c == 41) {
$('.c').text(n);
}
}, 1000);
}
setInterval(function() {
myWindow.close();
}, 45000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="idview" type="submit">
Submit
</button>