Frage

Ok, ich habe mich eine Weile mit diesem Problem beschäftigt und Folgendes zusammengetragen:

  1. Wenn Sie in IE7 einen AJAX-Aufruf durchführen und eine window.onbeforeunload-Funktion angegeben haben, ruft dieser die onbeforeunload-Funktion auf.

  2. Wenn Sie versuchen, mit window.open ein neues Fenster zu öffnen, OHNE das aktuelle Fenster zu stören, wird onbeforeunload aufgerufen.

Weiß jemand, wie man das stoppen kann?Ich habe sogar versucht, eine Variable auf TRUE zu setzen und diese Variable in meiner Funktion onbeforeunload zu überprüfen, aber es funktioniert immer noch nicht!Ich muss nur in der Lage sein, die Ausführung dieser Methode für AJAX-Aufrufe und neue Fensteraufrufe zu stoppen.

War es hilfreich?

Lösung

Eine andere und wahrscheinlich einfachere Option ist es, false zurückzugeben, wenn Sie das Popup öffnen:

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

Dies scheint zu verhindern, dass der IE davon ausgeht, dass Sie die Seite verlassen und das Ereignis auslösen. Alle anderen Optionen waren für mich nicht besonders lebensfähig.

Andere Tipps

Ok, ich hatte dieses Problem. Ich habe eine (ziemlich chaotische) Arbeit dafür.

In meinem Fall möchte ich die Navigation manchmal und nicht andere blockieren.

Also setze ich eine Flagge im Fenster ein, um mir zu sagen, ob ich es blockiert würde. Also, wo Sie Ihr Fenster machen.

Ich habe das Java -Skript (Showhelp) von einem Link ausgeschaltet:

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

OnbeForeUnload wird vor dem Showhelp aufgerufen, daher habe ich das Onclick verwendet, um die Flagge zu setzen

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

Hässlich wie Sünde, aber es scheint zu funktionieren!

Dies ist keine Lösung, sondern eine Erklärung für alle, die es interessiert.Ich habe gerade einen Schnelltest in IE 7 durchgeführt und er löst jedes Mal das Ereignis „onebeforeunload“ aus, wenn auf einen Link geklickt wird, es sei denn, die HREF befindet sich irgendwo auf derselben Seite:d.h.es sei denn, es enthält ein #.Ich vermute also, dass die IE-Ingenieure dachten, wenn jemand auf einen Link klickt, der nicht zu einer anderen Stelle auf der Seite führt, muss er die Seite verlassen. In diesem Fall wird die Seite gleich entladen.Es versteht sich von selbst, dass es bei dieser Denkweise offensichtliche Probleme gibt.

Ich dachte mir, dass Sie das Fenster nur verunreinigen müssen.

Ich habe gerade die Funktion in IE deaktiviert.

Ich hatte das gleiche Problem, in meinem Fall kamen alle Anfragen von einem Ajax-Aufruf, das vereinfacht die Lösung, denn als ich das Problem mit der Standardschaltfläche behoben habe, habe ich eine rekursive Funktion ausgeführt, um alle Klicks auf meine zentrale Funktion umzuleiten und dann den rechten Klick abzuleiten.Ich kopiere auch die Lösung für das Href-Problem mit Ajax-Aufrufen.Diese Lösung verhindert die Rückkehr zur vorherigen Seite.Legen Sie den Code in eine Datei namens Backbutton ein, JS JEDEM BEMETZUNG SCHREIBEN MIT MPARMA@USA.NET mit Betreff: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;
}

Sid_M sagte einen berechtigten Punkt: Bei diesem Browserdesign müssen wir unsere Anwendung entsprechend gestalten.

Warum sollten Sie sich für Anker-Tags und href für Popup-Fenster entscheiden? Verwenden Sie einfach die onclick-Methode im Label oder sogar das td-Tag und rufen Sie window.open auf

Einfaches Beispiel aus einer meiner Anwendungen

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

Haben Sie versucht, den Handler von "OnbeFeforeUnload" zu entfernen, bevor Sie zu window.open anrufen? Das kann helfen, aber ich habe es nie getestet.

Dies könnte Ihr Problem sehr möglich lösen!

Weil ich ein ähnliches Problem hatte und das machte es!

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

Und wann immer ich einen Ajax -Anruf mache oder ein anderes Fenster popupiert, habe ich nur Warnrequent auf false gesetzt.

Eine andere Sache ist sich nur bewusst, wenn er einen Ajax -Anruf tätigt, dass er synchronisiert ist, sonst wurde die Variable noch nicht festgelegt! (Ein böser Gotcha!)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top