How To Disable Copy and Paste Using Javascript
Submitted by alpha_luna on Monday, June 6, 2016 - 15:39.
In this article, we are going to learn how to Disable Copy and Paste Using Javascript. This is a simple source code to disable copy and paste of your content to your website. We are going to use the JavaScript script to do this function.
So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.
Creating HTML Source Code
This is our sample content in a page.- <div style="height:480px; width:700px; margin:auto; padding:40px; border:2px solid #1982d1; border-radius:10px;">
- <center>
- </center>
- You can write your own content here. You can even have your own blog.
- <br />
- <br />
- You can write your own content here. You can even have your own blog.
- <br />
- <br />
- You can write your own content here. You can even have your own blog.
- <br />
- <br />
- You can write your own content here. You can even have your own blog.
- <br />
- <br />
- You can write your own content here. You can even have your own blog.
- <br />
- <br />
- You can write your own content here. You can even have your own blog.
- <br />
- <br />
- You can write your own content here. You can even have your own blog.
- <br />
- <br />
- You can write your own content here. You can even have your own blog.
- </div>
This is the result of the code above:
Creating JavaScript Script
In the code above, we are going to add the JavaScript script to disable copy and paste of the content of a page.- <!-- Disable Copy and Paste-->
- <script language='JavaScript1.2'>
- function disableselect(e){
- return false
- }
- function reEnable(){
- return true
- }
- document.onselectstart=new Function ("return false")
- if (window.sidebar){
- document.onmousedown=disableselect
- document.onclick=reEnable
- }
- </script>
- <script>
- //disable mouse drag select start
- document.onselectstart = new Function('return false');
- function dMDown(e) { return false; }
- function dOClick() { return true; }
- document.onmousedown = dMDown;
- document.onclick = dOClick;
- $("#content_document").attr("unselectable", "on");
- //disable mouse drag select end
- //disable right click - context menu
- document.oncontextmenu = new Function("return false");
- //disable CTRL+A/CTRL+C through key board start
- //use this function
- function disableSelectCopy(e) {
- // current pressed key
- var pressedKey = String.fromCharCode(e.keyCode).toLowerCase();
- if (e.ctrlKey && (pressedKey == "c" || pressedKey == "x" || pressedKey == "v" || pressedKey == "a")) {
- return false;
- }
- }
- document.onkeydown = disableSelectCopy;
- //or use this function
- $(function () {
- $(document).keydown(function (objEvent) {
- if (objEvent.ctrlKey || objEvent.metaKey) {
- if (objEvent.keyCode == 65 || objEvent.keyCode == 97) {
- return false;
- }
- //}
- }
- });
- });
- </script>
Add new comment
- 198 views