A Pen by Erik Van Till on CodePen.
Forked from recursiev/Trying-to-prevent-copying-of-text.markdown
Created
January 20, 2014 03:28
-
-
Save desilinguist/8514418 to your computer and use it in GitHub Desktop.
A Pen by Erik Van Till.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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