문제

좋아, 나는 이 문제에 대해 한동안 시간을 ​​보냈고 이것이 내가 수집한 것입니다:

  1. IE7에서 AJAX 호출을 하고 window.onbeforeunload 함수가 지정된 경우 onbeforeunload 함수가 호출됩니다.

  2. 현재 창을 방해하지 않고 window.open을 사용하여 새 창을 열려고 하면 onbeforeunload가 호출됩니다.

이것을 멈추는 방법을 아는 사람이 있나요?변수를 TRUE로 설정하고 onbeforeunload 함수에서 해당 변수를 확인해 보았지만 여전히 작동하지 않습니다!AJAX 호출과 새 창 호출에 대해 해당 메서드의 실행을 중지할 수 있으면 됩니다.

도움이 되었습니까?

해결책

팝업을 열면 또 다른 더 간단한 옵션이 거짓을 반환하는 것입니다.

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

이것은 당신이 페이지를 떠나고 이벤트를 트리거한다고 생각하는 것을 막는 것 같습니다. 다른 모든 옵션은 나에게 특히 실용적이지 않았습니다.

다른 팁

좋아, 나는이 문제를 겪고있다. 나는 그것을 위해 (다소 지저분한) 일을 가지고 있습니다.

제 경우에는 다른 사람들이 아닌 가끔 항법을 차단하고 싶습니다.

그래서, 나는 그것을 막기를 원한다면 창문에 깃발을 설정하고 있습니다. 그래서 당신이 당신의 Window.open을하고있는 곳에서, 그 직전에, 'wind

링크에서 Java Script (Showhelp)가 시작됩니다.

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

onbeforeunload는 쇼 헬프 전에 호출되므로 OnClick을 사용하여 플래그를 설정했습니다.

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

죄처럼 못 생겼지 만 효과가있는 것 같습니다!

이것은 해결책은 아니지만 관심 있는 사람을 위한 설명입니다.방금 IE 7에서 빠른 테스트를 실행했는데 HREF가 같은 페이지의 어딘가로 이동하지 않는 한 링크를 클릭할 때마다 onebeforeunload 이벤트가 발생합니다.즉.#이 포함되어 있지 않은 한.그래서 내 추측으로는 IE 엔지니어들은 누군가가 페이지의 다른 곳으로 연결되지 않는 링크를 클릭하면 페이지를 떠나야 하며, 이 경우 페이지가 언로드될 것이라고 생각하고 있었던 것 같습니다.말할 필요도 없이 이러한 생각에는 명백한 문제가 있습니다.

나는 당신이 창을 설정하지 않아야한다고 생각했다.

방금 IE의 기능을 비활성화했습니다.

나는 같은 문제가 있었습니다. 제 경우에는 모든 요청이 ajax 호출에서 나왔기 때문에 솔루션이 단순화되었습니다. 왜냐하면 표준 버튼으로 문제를 해결할 때 모든 onclick을 중앙 집중식 기능으로 리디렉션한 다음 rigth 클릭을 dispath하는 재귀 기능을 수행했기 때문입니다.href 문제를 지원하는 Ajax 호출에 대한 솔루션도 복사하고 있습니다.이 해결 방법은 이전 페이지로 돌아가는 것을 방지합니다.code를 backbutton, js라는 파일 안에 넣습니다.자바스크립트 뒤로버튼

<!--  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은 유효한 포인트라고 말했습니다. 브라우저 설계를 통해 응용 프로그램을 그에 따라 설계해야합니다.

팝업 창에 앵커 태그 및 href로 이동하는 이유, 라벨 또는 TD 태그 및 호출 창에서 Onclick 메소드를 사용하기 만하면됩니다.

내 응용 프로그램 중 하나의 간단한 예

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

Window.open으로 호출하기 전에 "onbeforeunload"이벤트에서 핸들러를 제거 해 보셨습니까? 이것은 도움이 될 수 있지만 나는 그것을 테스트하지 않았습니다.

이것은 당신의 문제를 해결할 수 있습니다!

나는 비슷한 문제가 있었기 때문에 이것은 길을 갔다!

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

그리고 Ajax 전화 또는 다른 창을 팝업 할 때마다 방금 거짓으로 경고를 설정했습니다.

Ajax 호출을 할 때 다른 것은 변수가 아직 설정되지 않았을 수도 있음을 알 수 있습니다! (불쾌한 Gotcha!)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top