Javascript problem!

what up people

Maybe a simple question for you… but its mind blowing for me! I have a piece of JavaScript that resizes to a photograph when you click on a thumbnail ok? all the thumbnails and images are loaded dynamically through php ok… my client wants to disable right click on the photos so no body can copy them, but i dont know how to do this with the script i have got!!!

The script is…

<script>

PositionX = 300;
PositionY = 150;

defaultWidth  = 400;
defaultHeight = 400;

var AutoClose = true;

// ================================

if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=100-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}

</script>

Any ideas?

Yeah, talk with your client and tell him/her that a script to disable right clicking isn’t going to do anything but annoy people. Including a script like that really doesn’t do any good as far as stopping people from getting the images if they really want them. Actually the top for this was recently discussed in another post: http://www.kirupa.com/forum/showthread.php?t=214692

Opps almost forgot. If you can’t convince your client that it’s a bad idea, here’s some code:

function click() {
  if (event.button==2 || event.button==3) alert('Error : Not Allowed!') ;
 }

document.onmousedown=click;

I have no clue as to how well that code will work. I haven’t used it in like 4+ years. Which means you may want to test it.

i dont think it will annoy the user… because what she does is photography, and the script is to stop people saving the photographs shes taken, becase the no right click is for a section of the site where her clients can log in and view the photographs of the wedding or whatever, and she dont want people stealing them.

any ideas though?

Well like I mention in the post I linked to before (http://www.kirupa.com/forum/showthread.php?t=214692) there are a lot of ways for users to get the images from a website if they want it. Even if you disable the right click someone can still view the source to find the path of the file, save the webpage (which usually allows them to save all the images on that page), or if worse comes to worse they can take a screen capture of the page and crop out the image.

The code I pasted may work to stop the right click - actually now that I look at it, it probably won’t work in Firefox since I don’t think it supports “event”. But you should be able to do a search for “javascript prevent right click” at google and get like 10 million+ links. :slight_smile:

If the images are placed online, people can get them if they want them badly enough. If you really want to prevent them from getting a good copy of the image you may just want to watermark the image so it’s really of no use to whoever does try to take it.

ok, she does want it though so thats the problem, and i dont know how i put that in the script… argh! any ideas?

Did you search for a script at google like I suggested? Something like so may work: http://www.java-scripts.net/javascripts/No-Right-Click.phtml

Just take that script and place it in between the head tags in the html of the page you want to use it on. Or do a search for a better prevent right click script if that one isn’t what you’re looking for or fails on certain browsers.

If you’re going to use it on a lot ofpages maybe just toss the script into a .js file - remove the script tags from it. Then just call it with:

<script type="text/javascript" src="include/file.js"></script>

include/file.js is the path and file name.

If you’re looking to use it on the html files you’re creating with your script:

Change:

writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');

To:

writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style><script type="text/javascript" src="file.js"></script>');writeln('<sc'+'ript>');

I believe that should about do it… didn’t test it though.

  • Yeah, you can always do that or use Flash if your client wouldn’t mind using Flash. A Flash gallery is pretty much your safest route in preventing users from stealing images.

i tried the include bit mate and it didnt work just gives me a mega error.

I have got a watermak BUT my client insists she wants a no right click on it… the popup is automatically generated by this script and resizes to the image so i cant just place a no right click script into the head… i have to do it with this writeIn crap

any ideas?

What’s the error? Hard to know what to fix if I don’t know what error you’re getting. :smiley:

Make sure to check the code I gave you since I just copied and pasted from a template I have. Typically I keep all my .js, .css and image files in an “include” directory. Which the code I pasted refelects. Check to make sure that your code is actually looking for the file in the right place (right directory, right file name).

Otherwise, post more info on the error…