Hi guys :)

Is there a chance to disable these two things from my site?
(View Source) and (Ctrl + C )

please, give me few minutes from your time and help, i'll be much more appreciated :)


thanks in advance :)

Recommended Answers

All 23 Replies

For this I use onKeyDown event. See the following code
that disables CTRL C and CTRL V.

Note that this is not enough to prevent COPY/PASTE
in a text field, since the user can use CTRL INS, SHIFT INS
or simply use the context menu (right click on text field).

<html>
<head>
<script language="javascript">

function onKeyDown() {
  // current pressed key
  var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();

  if (event.ctrlKey && (pressedKey == "c" || 
                        pressedKey == "v")) {
    // disable key press porcessing
    event.returnValue = false;
  }

} // onKeyDown

</script>
</head>

<body>
<form name="aForm">
<input type="text" name="aText" onkeydown = "onKeyDown()">
</form>
</body
</html>

how to disable ctrl+c:
http://www.faqs.org/faqs/msdos-programmer-faq/part2/section-16.html

And
That shortcut is handled by the operating system. It never reaches the browser and so the browser has no opportunity to pass it to the web page.

IE only:

Add the following code to your BODY tag:

ondragstart="return false" onselectstart="return false"

commented: Thanks for the big help :) +1

Thanks a lot you did helped me a lot that's nice from you :D

thanks a lot, much appreciated :)

Fine....

As long you do not provide pure HTML site nothing is as it seems in source view...

Shanti Chepuru

u done good work

Yeah, he did :)

thanks for ur compliments...

Yeah, he did :)

And im female....

That's awesome :D
never seen before a female programmer :D

that's cool :D

thank you...

This code to disable ctrl (control).once you disable the ctrl other all event such as ctrl+C,ctrl+A etc. are disable.

<script language='javascript'> //function for to disable ctrl event of the page.
	function document.onkeydown() 
	{ 
		if ( event.keyCode==17) //17 is ascii code for ctrl
		{ 
			event.keyCode = 0; 
			event.cancelBubble = true; 
			return false; 
		} 
	}
</script>

This code to disable ctrl (control)

The bad news is that your code is not cross-browser compatible.
Even in IE, it doesn't prevent copying with other keystrokes or with the mouse. It also doesn't deal with <iframe>s [and possibly <frame>s as well, but I didn't bother to test].

The good news is that this thread had been dead for almost two years before you resurrected it, so the prior participants are unlikely to care. Plus, it gives a link to a page which explains in some detail why the 'view source' part of the request [which you didn't address] is not possible.

Shanti's Attempt is quite good..
But how to disable Copy and Paste events using Mouse..

garyrichard, disable the mouse right click with:

<script>
var isNS = (navigator.appName == "Netscape") ? 1 : 0;

if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);

function mischandler(){
return false;
}

function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;

</script>

In addition to the above example you can also add the below commands into your HTML <body> tag. Which will disable the keyboard and mouse in Internet Explorer.

oncontextmenu="return false" onselectstart="return false" ondragstart="return false"

Thanks a lot Quasimodo8. it's working fine.

Member Avatar for Ahmad_10

Just change your tag <body> or <body whatever here> with

<body expr:class='&quot;loading&quot; + data:blog.mobileClass' oncontextmenu='return false;' onkeydown='return false;' onmousedown='return false;' style='-moz-user-select: none;'>
Member Avatar for Ahmad_10

But it's still easy, some people just use view-source:yourUrl, I just need the answer about how to disable view-source:Url or encript our html code, thanks

@Ahmad_10, please create a new post instead of pull a very old post up.

If you want to disable view source from a browser menu, you will have to force open a new window with menubar/toolbar disabled. However, it is still defeated easily if the person really knows how to deal with "javascript." Therefore, there is no permanent way to do so.

You cannot prevent a user from seeing or copying the source code of your web pages. You can do some things to make it difficult for a human to read the code, but they don't really help much. In the end, you have to send valid HTML to the user's browser, and all the user has to do is select "Save" from the File menu and he has your code on his computer where he can do as he pleases with it.

I used these code below to protect my salespage

<!-- a code to show alert to enable javascript --> <div align="center"><noscript> <div style="position:fixed; top:0px; left:0px; z-index:3000; height:100%; width:100%; background-color:#FFFFFF"> <div style="font-family: Arial; font-size: 17px; background-color:#00bbf9; padding: 11pt;">Mohon aktifkan javascript pada browser untuk mengakses halaman ini!</div></div> </noscript></div> <!-- a code to protect from text selection, block text etc --> <script type="text/javascript">
function disableSelection(e){if(typeof e.onselectstart!="undefined")e.onselectstart=function(){return false};else if(typeof e.style.MozUserSelect!="undefined")e.style.MozUserSelect="none";else e.onmousedown=function(){return false};e.style.cursor="default"}window.onload=function(){disableSelection(document.body)}
</script> <!-- a code to protect content from right click --> <script type="text/javascript">
function mousedwn(e){try{if(event.button==2||event.button==3)return false}catch(e){if(e.which==3)return false}}document.oncontextmenu=function(){return false};document.ondragstart=function(){return false};document.onmousedown=mousedwn
</script> <!-- a code to protect shorcut keyboard, view source etc --> <script type="text/javascript">
window.addEventListener("keydown",function(e){if(e.ctrlKey&&(e.which==65||e.which==66||e.which==67||e.which==73||e.which==80||e.which==83||e.which==85||e.which==86)){e.preventDefault()}});document.keypress=function(e){if(e.ctrlKey&&(e.which==65||e.which==66||e.which==67||e.which==73||e.which==80||e.which==83||e.which==85||e.which==86)){}return false}
</script> <!-- a code to protect your articles --> <script type="text/javascript">
document.onkeydown=function(e){e=e||window.event;if(e.keyCode==123||e.keyCode==18){return false}}
</script>

I'm aware that this has already been answered, but I just thought I'd drop a few different methods here for restricting access to a website's source code. While it is impossible to fully eliminate access, you can definitely make it harder.

Using CSS to Disable Selecting:

* {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: -moz-none;
    -o-user-select: none;
    user-select: none;
}
/* Makes all objects on the site unable to be selected, the asterisk can be changed to whatever elements you wish to select */

Using JS to disable keybinds (already shown by other users):

//Basic key disabling
document.onkeydown = function(e) {
    if (event.keyCode === 123) {
        return false //Disables inspect element via F12
    }
    if (e.ctrlKey && e.shiftKey && e.keyCode === "I".charCodeAt(0)) {
        return false //Disables inspect element via ctrl+shift+i
    }
    if (e.ctrlKey && e.shiftKey && e.keyCode === "J".charCodeAt(0)) {
        return false //Disables inspect element via ctrl+shift+j
    }
    if (e.ctrlKey && e.keyCode == "U".charCodeAt(0)) {
        return false //Disables viewing source via ctrl+u
    }
    if (e.ctrlKey && e.keyCode == "C".charCodeAt(0)) {
        return false //Disables copying via ctrl+c
    }
}

//A function to more easily block whatever keybinds you want
var keybinds = []
function block(condition) {
    keybinds.push(condition)
}
document.onkeydown = function (e) {
    for (let i = 0; i < keybinds.length; i++) {
        if (eval(keybinds[i])) {
            return false
        }
    }
}

//Function example
block(`e.ctrlKey && e.keyCode == "V".charCodeAt(0)`) //Blocks ctrl+v

Using HTML to disable right clicking:

<body oncontextmenu="return false">

Hiding script tags from the inspector (can still view via source):

<html>
    <head>
        <title>Example page</title>
        <script class="disposable">
            //Your JS code
        </script>
        <!-- Other head stuff -->
    </head>
    <body>
        <!-- Your body code -->
        <script class="disposable">
            for (let i = 0; i < document.getElementsByClassName("disposable").length; i++) {
                document.getElementsByClassName("disposable")[0].remove()
            }
            //Make sure this is the last script tag on the page so that it doesn't interfere with other code
        </script>
    </body>
</html>

Of course, if these solutions aren't enough, you can always create your own encryption function or use some other creative method to hide your code. Some neat ideas include using the canvas tag to do pixel manipulation, allowing you to store your code inside of an image.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.