Skip to content

Instantly share code, notes, and snippets.

@qborreda
Last active August 29, 2015 14:04
Show Gist options
  • Save qborreda/fdfd0a22bb1d3408b9c3 to your computer and use it in GitHub Desktop.
Save qborreda/fdfd0a22bb1d3408b9c3 to your computer and use it in GitHub Desktop.
Various javaScript methods to prevent user control and menus ..
// Disable Context Menu
document.oncontextmenu = function () {
return false
};
// Disable dragging of HTML elements
document.ondragstart = function () {
return false
};
// Break out of frames
function bust() {
document.write = "";
window.top.location = window.self.location;
setTimeout(function () {
document.body.innerHTML = ''
}, 0);
window.self.onload = function (evt) {
document.body.innerHTML = ''
}
}
if (window.top !== window.self) {
try {
if (window.top.location.host) {} else {
bust()
}
} catch (ex) {
bust()
}
}
// Annoy user if context menu button presssed
document.onkeydown = function (e) {
if (e.keyCode == 93) {
alert('_')
}
};
// Disable selecting of text
document.onselectstart = function () {
if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") {
return false
} else {
return true
}
};
// Disable Mousedown event, except for text-areas & Inputs where it's needed
document.onmousedown = function (e) {
var obj = e.target;
if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") {
return true
} else {
return false
}
};
// Stop CTRL Key abuse on your page!
window.onkeydown = function (e) {
if (e.ctrlKey == true) {
return false
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment