Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing F5 inside an iFrame using jQuery

Our application has a requirement that users are not allowed to refresh a webpage using F5.

I've implemented the solution here however I've just discovered that this does not work inside an iframe on the page.

The iframe is generated from a telerik radeditor control.

I've tried doing the following inside my page containing the radeditor.

$(function () {
    $('#<%=RadEditor1.ClientID %>_contentIframe html').bind('keydown', function () {
        if (window.event && window.event.keyCode == 116) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
            window.event.keyCode = 0;
            window.status = "F5 is disabled on all popups";
            return false;
        }
    });
});

However it is not working.

Is there something special about iframes that I am missing?

like image 434
Biff MaGriff Avatar asked Mar 26 '26 08:03

Biff MaGriff


2 Answers

Knowing that you want to prevent users from losing data while they are working on the page, I'd propose a different solution. Why not just hook into when they leave the page, and prompt to see if they meant to do that?

Best way to detect when a user leaves a web page?

Gmail uses this method when messages haven't saved/been sent yet. This will help you cover many scenarios, keep your users happy, and still allow F5 to work if someone desires.

like image 160
Brad Avatar answered Mar 27 '26 21:03

Brad


This method works consistently.

$(function () {
    function addF5Handler(){
        el = $('body', $('#myframe').contents());
        if(el.length != 1) {
            setTimeout(addF5Handler, 100);
            return;
        }
        $($('#myframe').get(0).contentWindow.document).keydown(function () {
            var myWindow = $('#myframe').get(0).contentWindow;
            if (myWindow.event && myWindow.event.keyCode == 116) {
                myWindow.event.cancelBubble = true;
                myWindow.event.returnValue = false;
                myWindow.event.keyCode = 0;
                myWindow.status = "F5 is disabled on all popups";
                return false;
            }
        });
    }
    addF5Handler();
});
like image 27
Biff MaGriff Avatar answered Mar 27 '26 20:03

Biff MaGriff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!