简体   繁体   English

在Silverlight中禁用F5

[英]Disable F5 in Silverlight

Some of my users accidentally hit F5 when they are typing, and cause them to lose all the stuff they have typed. 我的一些用户在键入时不小心碰到了F5,导致他们丢失了所有键入的内容。 I do not need to prevent hitting Refresh button. 我不需要阻止点击“刷新”按钮。

I tried to use the following javascript, but it only works when the user does not focus on the Silverlight app (ie it works when the user click on somewhere outside of the SL app, but the onkeydown event is not triggered when the user focus on the SL). 我尝试使用以下javascript,但是它仅在用户不关注Silverlight应用程序时才起作用(即,当用户单击SL应用程序之外的某个地方时它起作用,但是当用户将注意力集中在Silverlight应用程序上时,不会触发onkeydown事件SL)。

document.onkeydown=function(e) {
    var event = window.event || e;
    if (event.keyCode == 116) {
        event.keyCode = 0;
        alert("test");
        return false;
    }
}

Probably the best way of handling this situation would be to use the onbeforeunload event and ask the user for confirmation. 处理这种情况的最佳方法可能是使用onbeforeunload事件并要求用户进行确认。 This way you can root out the accidental refreshes or closed tabs from the legitimate ones without handling all possible shortcuts. 这样,您可以从合法的行中清除意外的刷新或关闭的选项卡,而无需处理所有可能的快捷方式。

window.onbeforeunload = function(e) {
    return 'Are you sure you want to leave without saving your changes?';
};

You could even display the confirmation dialog only if there are some unsaved changes. 您甚至可以仅在某些未保存的更改时显示确认对话框。

I'd say, something like this? 我会说,像这样?

document.onkeydown = function() 
{
    switch (event.keyCode) 
    {
        case 116 : //F5 button
            event.returnValue = false;
            event.keyCode = 0;
            return false;
        case 82 : //R button
            if (event.ctrlKey) 
            {
                event.returnValue = false;
                event.keyCode = 0;
                return false;
            }
    }
}

note: ctrl + r is a shortcut to refresh as well 注意:ctrl + r也是刷新的快捷方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM