Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desilinguist/8514418 to your computer and use it in GitHub Desktop.
Save desilinguist/8514418 to your computer and use it in GitHub Desktop.
A Pen by Erik Van Till.
<input type="text" value="Some random non editable text" readonly="true" size="40" id="txt" />
<div id="container">Try copying this text.
<div id="innerContainer"></div>
</div>
//Prevent copy paste via HTML attributes
var body = document.body;
body.setAttribute("oncopy","return false");
body.setAttribute("oncut","return false");
body.setAttribute("onpaste","return false");
//This prevents the action of right clicking
//This is now pointless after what I implemented above
var preventRightClick = window.addEventListener("contextmenu",function(e) {
e.preventDefault();
alert("Not allowed to right click, sorry");
});
//This prevents the Ctrl+C keyboard shortcut...
//Umm...ok...No it doesn't :-/
var preventCtrlC = window.addEventListener("keypress",function(e) {
var str;
for (i in e) {
str = str + i + ": " + e[i] + "<br />";
}
document.getElementById("innerContainer").innerHTML = str;
});
#txt {
width: 200px;
margin: 20px auto;
position: relative;
}
#container {
width: 500px;
height: 200px;
position: relative;
margin: 100px auto;
text-align: center;
font-size: 2em;
}
/*This hides the highlight color when you try to highlight with mouse*/
#container::selection {
background: rgba(0,0,0,0);
}
#container::-moz-selection {
background: rgba(0,0,0,0);
}
#innerContainer {
margin: 50px auto;
font-size:14px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment