2009-11-01 16 views
11

Ok, ho trascorso un po 'su questo problema e questo è quello che ho raccolto:Internet Explorer chiamando window.onbeforeunload su window.open e chiamate AJAX

  1. Se si effettua una chiamata AJAX in IE7 e hai una funzione window.onbeforeunload specificata, chiama la funzione onbeforeunload.

  2. Se si tenta di aprire una nuova finestra con window.open SENZA disturbare la finestra corrente, viene richiamato onbeforeunload.

Qualcuno sa come fermarlo? Ho anche provato a impostare una variabile su TRUE e controllare quella variabile nella mia funzione onbeforeunload e funziona ancora dosent! Devo solo essere in grado di interrompere l'esecuzione di quel metodo per le chiamate AJAX e le nuove chiamate sulla finestra.

+0

Appena visto lo stesso problema! WTF è IE7 che sta chiamando window.onbeforeunload quando apri una finestra NEW e quella attuale è invariata! Non posso crederci ..... –

+0

davvero! ma che altro ti aspetti da IE xD ... e il problema è ancora lì in IE8. – agentfll

+0

Sì, mi sono imbattuto anche in questo. In IE8 – Jon

risposta

7

Un'altra opzione e probabilmente più semplice è quella di restituire false quando si apre il popup:

<a onclick="window.open(...); return false;" href="javascript:;" >my link</a> 

Questo sembra smettere di IE di pensare si sta lasciando la pagina e attivando l'evento. Tutte le altre opzioni non erano particolarmente praticabili per me.

0

hai provato a rimuovere il gestore dall'evento "onbeforeunload" prima di chiamare a window.open? Questo può aiutare ma non l'ho mai testato.

1

Ho pensato che devi solo disinserire la funzione window.onbeforeunload prima di fare qualsiasi cosa e poi rimetterla quando hai finito.

Ho appena finito di disabilitare la funzionalità in IE.

3

OK, ho riscontrato questo problema. Ho un (piuttosto disordinato) lavoro per questo.

Nel mio caso, voglio bloccare la navigazione a volte, e non altri.

Quindi, sto impostando un flag sulla finestra per dirmi se lo voglio bloccato. Quindi, dove stai facendo window.open, poco prima, fai 'window.allowExit = true' quindi nel onbeforeunload, controlla per window.allowExit = true.

ho il java script (ShowHelp) viene dato il via da un collegamento:

<a href="javascript:ShowHelp('argument')" >HERE</a> 

onbeforeunload viene chiamato prima lo ShowHelp, così ho usato l'onclick per impostare il flag

<a onclick="window.allowExit = true;" href="javascript:ShowHelp('argument')" >HERE</a> 

Ugly come peccato, ma sembra funzionare!

2

Questa non è una soluzione, ma una spiegazione per chiunque sia interessato. Ho appena eseguito un rapido test in IE 7 e viene generato l'evento onebeforeunload ogni volta che viene fatto clic su un link a meno che l'HREF non vada da qualche parte sulla stessa pagina: cioè a meno che non contenga un #. Quindi la mia ipotesi è che gli ingegneri di IE pensassero che quando qualcuno fa clic su un link che non si trova da qualche altra parte sulla pagina, allora devono lasciare la pagina, nel qual caso la pagina sta per scaricarsi. Inutile dire che ci sono ovvi problemi con questo modo di pensare.

1

Ho avuto lo stesso problema, nel mio caso tutte le richieste provengono da una chiamata Ajax, questo semplifica la soluzione, perché quando ho riparato il tasto con il pulsante standard ho fatto una funzione ricorsiva per reindirizzare tutto onclick alla mia funzione centralizzata e quindi dispath il rigth click. Sto copiando anche la soluzione per un problema di chiamata ajax suporting href. Questa soluzione evita di tornare alla pagina precedente. Mettere il codice all'interno di un file chiamato BackButton, JS Ogni commento scrittura a [email protected] con oggetto: javascript BackButton

<!-- backbutton developed by Manuel Parma 2011-06-10 --> 
<!-- email: [email protected] --> 
<!-- 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; 
} 
0

Questo potrebbe molto possibile risolvere il problema!

Perché ho avuto un problema simile e questo ha fatto andare avanti!

window.onbeforeunload = warnFunction; 
var warnRequired = true; 
function warnFunction() { 
    if (warnRequired) return ("Please Stay! Don't go!"); 
    return ; 
} 

E ogni volta che faccio una chiamata Ajax o PopUp un'altra finestra, ho appena impostato warnRequired false.

Un'altra cosa è tenere presente quando si effettua una chiamata Ajax che è sincronizzata, altrimenti la variabile potrebbe non essere stata ancora impostata! (Un brutto Geck!)

1

Sid_M ha detto un punto valido, con tale progettazione del browser dobbiamo progettare la nostra applicazione di conseguenza.

Perché andare per tag di ancoraggio & href per le finestre popup, basta usare il metodo onclick in etichetta o addirittura tag td e chiamare window.open

semplice esempio da uno della mia applicazione

<td class="popuplink" onMouseOver="this.style.cursor='hand'" onclick="javascript:openMe('img/howitworks.png', 'pndpopup', 600,650)"><u> How it works</u></td> 
Problemi correlati