Skip to content

Instantly share code, notes, and snippets.

@roshbrahm
Created July 9, 2014 10:36
Show Gist options
  • Save roshbrahm/346b8fc81188d9643f63 to your computer and use it in GitHub Desktop.
Save roshbrahm/346b8fc81188d9643f63 to your computer and use it in GitHub Desktop.
Disable right click i.e. Context Menu and F5 key for refresh in AngularJs
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<title>AngularJs Directive for disabling Context Menu</title>
<style type="text/css">
*.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
/*
Introduced in IE 10.
See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
*/
-ms-user-select: none;
user-select: none;
}
</style>
<script type="text/javascript" src="angular.js"></script><!--local path for angular.js-->
</head>
<body>
<body class="unselectable" ng-right-click="false">
</body>
<script type="text/javascript">
var myapp = angular.module('myapp', []);
myapp.directive('ngRightClick', function($parse) {
return function(scope, element, attrs) {
var rightClickDisabled=attrs.ngRightClick;
if(! rightClickDisabled){
element.bind('contextmenu', function(event) {
scope.$apply(function() {
event.preventDefault();
});
});
element.bind('keydown', function(event) {
scope.$apply(function() {
if ((event.which || event.keyCode) == 116){ // f5 keyCode
event.preventDefault();
}
});
});
}
}
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment