Internet Explorer调用window.onbeforeunload on window.open和AJAX调用
好的,我在这个问题上花了一段时间,我收集了以下信息:Internet Explorer调用window.onbeforeunload on window.open和AJAX调用,ajax,internet-explorer,window.open,onbeforeunload,Ajax,Internet Explorer,Window.open,Onbeforeunload,好的,我在这个问题上花了一段时间,我收集了以下信息: 如果在IE7中进行AJAX调用,并且指定了window.onbeforeunload函数,它将调用onbeforeunload函数 如果尝试在不干扰当前窗口的情况下使用window.open打开新窗口,则会调用onbeforeunload 有人知道如何阻止这一切吗?我甚至尝试将一个变量设置为TRUE,并在onbeforeunload函数中检查该变量,但它仍然不起作用!我只需要能够停止对AJAX调用和新窗口调用执行该方法。在调用window.o
有人知道如何阻止这一切吗?我甚至尝试将一个变量设置为TRUE,并在onbeforeunload函数中检查该变量,但它仍然不起作用!我只需要能够停止对AJAX调用和新窗口调用执行该方法。在调用window.open之前,是否尝试从“onbeforeunload”事件中删除处理程序?这可能会有所帮助,但我从未测试过它。我认为您只需在执行任何操作之前取消设置window.onbeforeunload函数,然后在完成操作后将其放回原处
我刚刚禁用了IE中的功能。好的,我一直有这个问题。我有一个(相当混乱的)工作要做 在我的例子中,有时我想阻止导航,而不是其他 因此,我在窗口上设置了一个标志,告诉我是否要阻止它。因此,在执行window.open之前,先执行“window.allowExit=true”,然后在onbeforeunload中检查window.allowExit=true 我的java脚本(SHowHelp)是从以下链接启动的:
<a href="javascript:ShowHelp('argument')" >HERE</a>
onbeforeunload在ShowHelp之前被调用,所以我使用onclick来设置标志
<a onclick="window.allowExit = true;" href="javascript:ShowHelp('argument')" >HERE</a>
丑陋的罪恶,但它似乎工作 这不是一个解决方案,而是对任何感兴趣的人的解释。我刚刚在IE 7中运行了一个快速测试,它会在任何时候单击链接时触发onebeforeunload事件,除非HREF位于同一页面的某个位置:即,除非它包含#。所以我猜IE工程师们认为,当有人点击了一个不在页面其他地方的链接时,他们一定是离开了页面,在这种情况下,页面就要卸载了。不用说,这种想法存在着明显的问题 另一个可能更简单的选项是在打开弹出窗口时返回false:
<a onclick="window.open(...); return false;" href="javascript:;" >my link</a>
这似乎可以阻止IE认为您正在离开页面并触发事件。所有其他选项对我来说都不是特别可行。我也有同样的问题,在我的情况下,所有请求都来自ajax调用,这简化了解决方案,因为当我用标准按钮修复问题时,我执行了一个递归函数,将所有onclick重定向到我的集中式函数,然后显示右键单击。 我正在复制ajax调用支持href问题的解决方案。 此解决方案避免返回上一页。将代码放入名为backbutton,js的文件中 有什么意见要写信给我吗mparma@usa.net主题:javascript backbutton
<!-- backbutton developed by Manuel Parma 2011-06-10 -->
<!-- email: mparma@usa.net -->
<!-- Add the next line into the <body> section as a first line -->
<!-- <script type="text/javascript" src="<path>/backbutton.js"></script> -->
<!-- Address used when backtoLogin is 1 or when there is not previous page from site -->
var returningAddress = "http://my returning address"
<!-- Message to display when an external action (back button, forward button, backspace) move without press the application buttons -->
var backButtonMessage = "Using the browser's Back button may cause a loss in data. Please use the Back and Continue buttons at the bottom of the form."
<!-- 0=no / 1=yes (assume per default that the back button will be pressed -->
<!-- and come into action if this was NOT the case) -->
var backButtonPressed = 1;
<!--This var when is setted to 1 avoid to captureEvent set backbuttonPressed to 1, otherwise unload event cannot detect the right state of backButtonPressed-->
var onbeforeunloadeventFired = 0;
<!--Indicate to logic back to first page (login) when its value is 1 otherwise the logic back to previous page out of the site-->
var backtoLogin = 0;
var DoPostBackWithOptionsHook = null;
var DoPostBackHook = null;
<!-- Check the previous status -->
if (window.name == ""){
<!-- When window.name is empty, this is indicating the first call of site -->
window.name = "L0001";
}
else {
if (window.name == "L0000_back"){
<!-- In this condition the page is returning after a foward button press -->
setBackButton(0);
window.name = "";
<!-- the system reload the page to clean the data -->
window.location.href = returningAddress;
}
else {
if (window.name.indexOf("_back") > 4){
<!-- when the word back is present, the previous call is sending a message that the back button was pressed and the site is going out -->
<!-- get the internal counter -->
var sLastValue = modifynamevalue(0);
<!-- set the count to go back -->
var iCountBack = -(sLastValue * 1);
if (backtoLogin == 1) {iCountBack++;};
if (window.history.length - 2 < -iCountBack) {
iCountBack = -(window.history.length - 2);
}
<!-- the site is flag that first page needs to reload -->
window.name = "L0000_back";
setBackButton(0);
<!-- the site is returning to the first page or previous -->
window.history.go(iCountBack);
}
else {
<!-- increase the internal counter -->
var sLastValue = modifynamevalue(+1);
window.name = "L" + sLastValue;
}
}
}
<!-- Set the events needed to manage the back and forwar button situations -->
$(document).ready(function(){
if (typeof(Sys) == "object") {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
window.onbeforeunload = onbeforeunloadEvent;
window.onunload = unloadEvent;
DoPostBackWithOptionsHook = WebForm_DoPostBackWithOptions;
WebForm_DoPostBackWithOptions = WebForm_DoPostBackWithOptionsHook;
doPostBackHook = __doPostBack;
__doPostBack = __doPostBackHook;
}
});
function WebForm_DoPostBackWithOptionsHook(options) {
setBackButton(0);
return DoPostBackWithOptionsHook(options)
}
function __doPostBackHook(eventTarget, eventArgument) {
if (backButtonPressed == 1) {
setBackButton(0);
}
return doPostBackHook(eventTarget, eventArgument)
}
function beginRequest(sender, args) {
setBackButton(0);
<!-- setting onbeforeunloadeventFired = 1 I take care to avoid anyone changed the Backbutton until endrequest -->
onbeforeunloadeventFired = 1;
}
function endRequest(sender, args) {
onbeforeunloadeventFired = 0;
setBackButton(1);
}
<!-- unload event handler -->
function unloadEvent(evt) {
<!-- double coundition using onbeforeunloadeventFired == 1 garantee avoid problemas with redirect operations -->
if ((backButtonPressed == 1) && (onbeforeunloadeventFired == 1)) {
<!-- decrement the internal counter -->
var sLastValue = modifynamevalue(-1);
window.name = "L" + sLastValue + "_back";
}
if (DoPostBackWithOptionsHook !== null) {
WebForm_DoPostBackWithOptions = DoPostBackWithOptionsHook;
};
if (doPostBackHook !== null) {
__doPostBack = doPostBackHook;
};
}
<!-- on before unload -->
function onbeforeunloadEvent(evt) {
onbeforeunloadeventFired = 1;
if (backButtonPressed == 1) {
return backButtonMessage;
};
}
<!-- used to set right backButtonPressed-->
function setBackButton(value){
if (value == 0) {
backButtonPressed = 0;
}
else {
if (onbeforeunloadeventFired == 0) {
backButtonPressed = 1;
}
}
}
<!-- increment and decrment the internal counter stored into windows.name -->
function modifynamevalue(iIncrement){
var iCount = (window.name.substring(1, 5) * 1) + iIncrement;
if (iCount < 0) {
iCount = 0;
}
var sNewValue = iCount.toString();
sNewValue = "0000".substring(0, 4 - sNewValue.length) + sNewValue;
return sNewValue;
}
var returningAddress=”http://my 回信地址“
var backButtonMessage=“使用浏览器的后退按钮可能会导致数据丢失。请使用表单底部的后退和继续按钮。”
var backButtonPressed=1;
var onbeforeunloadeventFired=0;
var-backtoLogin=0;
var DoPostBackWithOptionsHook=null;
var DoPostBackHook=null;
如果(window.name==“”){
window.name=“L0001”;
}
否则{
如果(window.name==“L0000_back”){
后退按钮(0);
window.name=“”;
window.location.href=返回地址;
}
否则{
if(window.name.indexOf(“\u back”)>4){
var sLastValue=modifynamevalue(0);
var iCountBack=-(sLastValue*1);
如果(backtoLogin==1){iCountBack++;};
如果(window.history.length-2<-iCountBack){
iCountBack=-(window.history.length-2);
}
window.name=“L0000\u back”;
后退按钮(0);
window.history.go(iCountBack);
}
否则{
var sLastValue=modifynamevalue(+1);
window.name=“L”+sLastValue;
}
}
}
$(文档).ready(函数(){
if(typeof(Sys)=“对象”){
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
window.onbeforeunload=onbeforeunloadEvent;
window.onunload=卸载事件;
DoPostBackWithOptions hook=WebForm_DoPostBackWithOptions;
WebForm_DoPostBackWithOptions=WebForm_DoPostBackWithOptions挂钩;
doPostBackHook=uu doPostBack;
__doPostBack=uu doPostBackHook;
}
});
函数WebForm_doPostBack与选项挂钩(选项){
后退按钮(0);
返回带有选项挂钩的DoPostBack(选项)
}
函数\uuu doPostBackHook(eventTarget,eventArgument){
如果(按下BackButton==1){
后退按钮(0);
}
返回doPostBackHook(eventTarget、eventArgument)
}
函数beginRequest(发送方,参数){
后退按钮(0);
onbeforeunloadeventFired=1;
}
函数endRequest(发送方,参数){
onbeforeunloadeventFired=0;
后退按钮(1);
}
功能卸载事件(evt){
如果((backButtonPressed==1)和(&(onbeforeunloadeventFired==1)){
var sLastValue=modifynamevalue(-1);
window.name=“L”+sLastValue+“\u back”;
}
if(doPostBackWithOptions挂钩!==null){
WebForm_DoPostBackWithOptions=DoPostBackWithOptions挂钩;
};
if(doPostBackHook!==null){
__doPostBack=doPostBackHook;
};
}
函数onbeforeunloadEvent(evt){
onbeforeunloadeventFired=1;
如果(按下BackButton==1){
返回backButtonMessage;
};
}
功能设置按钮(值){
如果(值==0){
backButtonPressed=0;
}
否则{
如果(onbeforeunloadeventFired==0){
背扣
window.onbeforeunload = warnFunction;
var warnRequired = true;
function warnFunction () {
if (warnRequired) return ("Please Stay! Don't go!");
return ;
}
<td class="popuplink" onMouseOver="this.style.cursor='hand'" onclick="javascript:openMe('img/howitworks.png', 'pndpopup', 600,650)"><u> How it works</u></td>