开发者

How to restrict Ctrl N combination in a browser window?

开发者 https://www.devze.com 2023-01-05 23:29 出处:网络
As per my knowledge we can disable ctrl+N key for new window with the following Javascript code: document.onkeydown = function() {

As per my knowledge we can disable ctrl+N key for new window with the following Javascript code:

document.onkeydown = function() { 
    alert(event.keyCode) 
    if ((event.keyCode == 78) && (event.ctrlKey)) { 
        alert ("No new window") 
        event.cancelBubble = true; 
        event.returnValue = false; 
        event.keyCode = false; return false; 
    } 
}

In my case开发者_如何学运维 there are 3 frames with one of them fixed (menu bar where I can put this code), but this event is not caught if the focus is in some other frame. How can I implement this restriction globally on a browser window?


you'll have to include the script for each document (frame). if all the frames are pulling from the same domain, you should be able to do this from a single point of entry, using the window.frames collection and working your way down to the document object for each frame.

also your script only works in IE.

also this is a terrible idea. you shouldn't be taking over people's browser functions.


Put your code in common .js (javascript) file and link that .js with all the pages. It will help in case if you want to remove the functionality or add any centralize functionality.


I don't think you are allowed to control the browser in this way from a web page (nor should you be)


While concerns such as these are often legitimate, trying to do things such as preventing key combos, or a right-click are actually bad for your site. Consider this:

A user expects their browser to work in a certain way. When you tamper with this functionality, the user can become frustrated. They may think that your site is broken, and leave.

When you attempt to prevent a user from performing built-in actions, you are essentially policing their behavior. This conveys to the user that you do not trust them to use your site unless under your supervision. The user will feel insulted, and leave.

What happens if a user has JavaScript disabled? Or if they have a custom rule-set that prevents websites from changing their browser's behavior?

What's so bad abut opening a new window? If it's a security concern, then that should be properly fixed. If the user has these thoughts they may feel insecure, and leave.

These (among many other) things must all be considered before launching a site. Hopefully this helps you while working on your project.

0

精彩评论

暂无评论...
验证码 换一张
取 消