Link to home
Start Free TrialLog in
Avatar of ajosephson
ajosephson

asked on

Disable right click on images to stop saving.

I have seen this done on some sites..

How is it done?

I appreciate your help..

AJ
Avatar of AzraSound
AzraSound
Flag of United States of America image

it is an annoyance....if someone really wants your images, they can get them.  they are allowed to download them to their browser, so the images are their's if they want them.  i think i read about some server software you could install that protects images, but it went for a hefty sum
Avatar of ajosephson
ajosephson

ASKER

I understand this... I am simply trying to inhibit the process. I appreciate computer savvy users will still find a way to capture the image...

AJ
Thanks for the info.. It works OK, but when I try to use  it with this code writen by ASPGuru (to hide the image URL) -

=====> pic.asp:
<%
Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.GetFile(Server.mapPath("secretDir/img.jpg"))

Set objStream = objFile.OpenAsTextStream(1, TristateTrue)

Response.ContentType = "image/jpeg"
Response.AddHeader "content-length", objFile.Size

Do While Not objStream.AtEndOfStream
 char = chrB(asc(objStream.Read(1))) 'converting unicode to single Byte
    Response.BinaryWrite(char)
Loop
%>


=====> test.html:
<HTML>
<BODY>
<img src="pic.asp">
</BODY></HTML>


Error - 'The HTTP headers are already written to the client browser' is returned.

Can the java script be simplified to disable right-click all together?
this script shows how to achieve this for both the full page and just images...hopefully the former will work with your current implementation:

http://www.web-wise-wizard.com/javascript-tutorials/disable-right-click.html
Avatar of simonet
ajosephson,

the code ASPGuru gave you will work, but you have to make the line below the very first line of your script:

<% Response.Buffer = True %>

This will force IIS not to write the headers as they are defined, but buffer the page and only write headers after the entire script has run.

Note that that code only hides the URL, but it doesn't prevent users from saving the image if they want to. The best combination - almost fail safe - is ASPGuru's code along with some client-side scripting (like AzraSound has pointed you to).

Alex
Thanks for the advice.. If I include <% Response.Buffer = True %> gibberish is returned.. I guess as the image is being sent (byte by byte?)

Here is how I've pieced it together -


<% Response.Buffer = True %>


<script language="JavaScript">
window.onload = trap_page_mouse_key_events;
</script>


<script language="JavaScript">

function disable_right_click(e)
{
    var browser = navigator.appName.substring ( 0, 9 );
    var event_number = 0;
    if (browser=="Microsoft")
        event_number = event.button;
    else if (browser=="Netscape")
        event_number = e.which;

    if ( event_number==2 || event_number==3 )
        {
        alert ("Right Mouse Button Is Disabled");
        return (false);
        }

    return (true);
}

function check_mousekey ()
{
    var mouse_key = 93;
    var keycode = event.keyCode;

    if ( keycode == mouse_key )
        alert ( "Mouse Key Is Disabled" );
}

function trap_page_mouse_key_events ()
{
    var browser = navigator.appName.substring ( 0, 9 );

    document.onmousedown = disable_right_click;

    if ( browser == "Microsoft" )
        document.onkeydown = check_mousekey;
    else if ( browser == "Netscape" )
        document.captureEvents( Event.MOUSEDOWN );
}

</script>


<%
pic = "picture.jpg"
Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")


Set objFile = objFileSystem.GetFile(Server.mapPath(pic))



Set objStream = objFile.OpenAsTextStream(1, TristateTrue)

Response.ContentType = "image/jpeg"
Response.AddHeader "content-length", objFile.Size

Do While Not objStream.AtEndOfStream
 char = chrB(asc(objStream.Read(1))) 'converting unicode to single Byte
    Response.BinaryWrite(char)
Loop

%>


If the buffers line is removed -

Response object error 'ASP 0156 : 80004005'

Header Error

/sunscripts/aj/pic.asp, line 68

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content


is displayed..

I appreciate your help..

AJ

ASKER CERTIFIED SOLUTION
Avatar of Moondancer
Moondancer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Points refunded and question closed.

Netminder
Community Support Moderator
Experts Exchange