2011-08-25 117 views
0

嗨,我卡在这一点。如何在窗口关闭时打开彩盒。jquery colorbox打开窗口关闭

我可以点击任何div类打开一个colorbox。

我想要一个colorbox在用户点击浏览器或窗口的关闭按钮时弹出。

任何帮助在这里将不胜感激。

由于提前

回答

0

你的意思window.onbeforeunload

 window.onbeforeunload = function() { 
      // do something here 
     }; 
+0

这不会,如果他们关闭唯一的浏览器窗口 – JamesHalsall

+0

它的工作在IE9工作,铬和FF 6.其他的人我没查。 – jani

+0

“工作”是什么意思? 如果用户决定这样做,用户仍然可以离开该页面,唯一发生的事情就是浏览器弹出问题,他确信这一点。 – WTK

0

通常,您不能通过向用户显示colorbox(或其他任何相关内容)来停止用户离开页面。

然而,也有可以听onbeforeunloadonunload的两个事件。 通过在beforeunload事件的处理函数中设置事件对象的returnValue属性,您可以告诉浏览器弹出一个问题窗口,询问用户是否想要留在本站点或离开。如果用户决定保持onunload状态,但如果他决定离开,则不会触发事件,但无法取消该操作,只能延迟实际休假。您可以通过使用alert()或某些运行指定时间的循环来延迟离开。检查下面的例子。

// showLeaveWarning is some imaginary variable that determines wherever you want to show the warning about leaving the page 
beforeUnloadHandler = function(e) { 
    if (showLeaveWarning) {    
     var e = e || window.event, msg = "Don't leave, please!" 
     if (e) { 
      e.returnValue = msg; // Chrome won't respect the msg you're trying to show but Firefox will 
     } 
     return msg; 
    } 
} 
unloadHandler = function(e) { 
     // you can do something here - like sending an ajax request to your application to know that user X has left the page 
     var a = (new Date()).getTime() + 1500; // 1500 is a number of ms that loop will run for 
     // the loop below is necessary to try to force the browser to wait couple more seconds while (for example) the ajax request is finished 
     while ((new Date()).getTime() < a) {} // do this loop for 1500ms (Firefox v6 seems to wait for as long as you please (by changin 1500 to something else in line above) 
    } 
} 
window.onunload = unloadHandler; 
window.onbeforeunload = beforeUnloadHandler;