وضع onbeforeunload على العنصر الأساسي في كروم وإنترنت إكسبلورر باستخدام مسج

StackOverflow https://stackoverflow.com/questions/1802930

سؤال

ولدي نظام حيث أريد أن تحقق مع المستخدم اذا كانوا على يقين من أنها تريد مغادرة الصفحة بمجرد تعيين إشارة القذرة.

وأنا باستخدام التعليمات البرمجية التالية - في فايرفوكس، ويمكنني أن ننظر في مصدر الصفحة من خلال مصاب بهوس الاحراق وعلامة لديه سمة onbeforeunload إدراجها في بشكل صحيح.

في كروم وفايرفوكس، وهذا لا يحدث على الرغم من وأنا قادرة على الانتقال من الصفحة دون أي تحذير على الإطلاق. بالتأكيد يجري تنفيذ خط مسج لتحديث الكود الجسم، فإنه ليست مجرد تنفيذ ذلك.

if ($("body").attr('onbeforeunload') == null) {
    if (window.event) {
        // IE and Chrome use this
        $("body").attr('onbeforeunload', 'CatchLeavePage(event)');
    }
    else {
        // Firefox uses this
        $("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
    }
}

وأي أفكار كيفية المضي قدما من هنا؟

هل كانت مفيدة؟

المحلول

ولا يمكنك إجهاض تفريغ الصفحة من خلال العودة كاذبة. يجب العودة السلسلة التي سيتم عرضها على المستخدم في مربع رسالة، وقال انه تقرر ما اذا كان يريدون مغادرة أو البقاء على صفحة (من خلال اختيار إما "موافق" أو "إلغاء" زر)، لذلك تحتاج إلى كتابة التعليمات البرمجية مثل هذا:

 window.onbeforeunload = function() {
    return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
 };

نصائح أخرى

وهذه محاولة

  <script type=\"text/javascript\">
        var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
        var leave_message = 'You sure you want to leave?'
        function goodbye(e) 
        {
            if(dont_confirm_leave!==1)
            {
                if(!e) e = window.event;
                //e.cancelBubble is supported by IE - this will kill the bubbling process.
                e.cancelBubble = true;
                e.returnValue = leave_message;
                //e.stopPropagation works in Firefox.
                if (e.stopPropagation) 
                {
                    e.stopPropagation();
                    e.preventDefault();
                }

                //return works for Chrome and Safari
                return leave_message;
            }
        }   
    window.onbeforeunload=goodbye;
    </script>
window.onbeforeunload = function () { return 'Are you sure?' };

وتحقق هذا الرمز:

var validNavigation = false;

function wireUpEvents() {
var dont_confirm_leave = 0; 
var leave_message = "You sure you want to leave ?";

function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave !== 1) {
if (!e) e = window.event;
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
}

window.onbeforeunload = goodbye;

document.onkeydown = function () {
switch (event.keyCode || e.which) {
case 116 : //F5 button
validNavigation = true;
case 114 : //F5 button
validNavigation = true;
case 82 : //R button
if (event.ctrlKey) {
validNavigation = true;
}
case 13 : //Press enter
validNavigation = true;
}

}
// Attach the event click for all links in the page
$("a").bind("click", function () {
validNavigation = true;
});

// Attach the event submit for all forms in the page
$("form").bind("submit", function () {
validNavigation = true;
});

// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function () {
validNavigation = true;
});
}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function () {
wireUpEvents();
});

وانها ليست جميلة، ولكنها فعلت الحيلة.

var warnclose = true;
var warn = function(e) {
    var warning = 'Your warning message.';
    if (warnclose) {
        // Disables multiple calls
        warnclose = false;

        // In case we still need warn to be called again 
        setTimeout(function(){
            warnclose = true;
        }, 500);

        return warning;
    }
};
window.onbeforeunload = warn;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top