PDA

View Full Version : Disable right click context menu in a pop up window.



Rivian
07-21-2005, 11:42 PM
Hi Everyone,

I'm somewhat new to Javascript. Today I added "larger image view" pop up windows to my online art gallery. The problem I'm having is I'm not sure how to disable the right click context menu in those pop up windows. :confused: I don't want to disable it for the rest of my site, just within the pop up menus themselves. Do you know of a script or a line of code that I could add to do this?

I'd really appreciate your help.

Thanks!!

~Rivian

jscheuer1
07-22-2005, 10:21 AM
This isn't a foolproof script by any means, there are many ways around it. But, if you are determined to use it, put the code to block the right click on the page where you want it active. If there is no page (as could be the case with an image only popping up in a new window), make one.

Rivian
07-22-2005, 03:22 PM
I'm not sure I entirely understand what you mean by make one.... How is it possible to make an entirely separate page for a pop up that I only want to call when someone clicks a thumbnail image so that they can see the larger version.

Can you explain what you mean and maybe give me an example? Is there a script to call pages like that?

Thanks,
~Rivian

jscheuer1
07-22-2005, 03:43 PM
Well, the most direct and cross browser friendly way is to make a page for each image. Here is a demo of another approach:

Opener (http://www.freewebs.com/jscheuer1/opener6.htm)

Rivian
07-22-2005, 06:02 PM
I definitely appreciate the idea... the problem I have with it is that I have over 400 images :eek: . If I were to make a page for each one not only would it take me forever, but it would use up sizable resources. Is there another way? :confused:

jscheuer1
07-22-2005, 06:26 PM
Did you check out the demo? There is but one page used to display all of the three different images. Anyways, if you cannot work it from there, I am afraid I am not much interested, as I think the whole idea of no right click is stupid to begin with. It won't protect anything and doesn't provide any visual benefit either. My time is better spent helping others.

Rivian
07-22-2005, 07:05 PM
I haven't disabled right click on any other portion of my site and have instead chosen to use other protection methods. I understand that many people have issues with disabling the context menu and I feel very much the same way. I just really want to disable it for my pop ups because I can't see how I can make any of my other security measures work in the pop up scenario that I already have. I was really hoping that there would be a snippet of code that I could add to my javascript to make this function properly.

I've seen other sites manage this somehow and I'm hoping that I can do the same. If anyone has any ideas, please, post a message. I'm desperate to do this for my artists so that they can have an easy to use interface and at the same time offer some measure of protection, even if it is easily circumvented by those who know how.

Thanks,
~Rivian

jscheuer1
07-22-2005, 07:48 PM
I've seen other sites manage this somehow . . .Name one. It is an illusion. They may have disabled right click in IE but, that doesn't protect their content.

Rivian
07-22-2005, 07:49 PM
Ok, so I answered my own question and for anyone else who comes across this problem, here's the fix.

<script type="text/javascript">

// JK Pop up image viewer script- By JavaScriptKit.com
// Visit JavaScript Kit (http://javascriptkit.com)
// for free JavaScript tutorials and scripts
// This notice must stay intact for use

var popbackground="black" //specify backcolor or background image for pop window
var windowtitle="Larger View" //pop window title

function detectexist(obj){
return (typeof obj !="undefined")
}

function jkpopimage(imgpath, popwidth, popheight, textdescription){

function getpos(){
leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
if (window.opera){
leftpos-=screenLeft
toppos-=screenTop
}
}

getpos()
var winattributes='width='+popwidth+',height='+popheight+',resizable=no,left='+leftpos+',top='+toppos
var bodyattribute=(popbackground.indexOf(".")!=-1)? 'background="'+popbackground+'"' : 'bgcolor="'+popbackground+'"'
if (typeof jkpopwin=="undefined" || jkpopwin.closed)
jkpopwin=window.open("","",winattributes)
else{
getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
jkpopwin.moveTo(leftpos, toppos)
jkpopwin.resizeTo(popwidth, popheight+30)
}
jkpopwin.document.open()
jkpopwin.document.write('<html><title>'+windowtitle+'</title><body '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em" onBlur="self.close()" onClick="self.close()" onContextmenu="return false;"><meta http-equiv="imagetoolbar" content="no"><br>'+textdescription+'</body></html>')
jkpopwin.document.close()
jkpopwin.focus()
}
</script>

The piece that I highlighted in orange is a snippet of code that I added to my script. This is all it took to disable the right click in my pop up ONLY. The rest of my page continues to use right click normally. YAY!!!

I wanted to thank you, John, for helping me. It pushed me to seek out my own solution :) and I really appreciate that!

~Rivian

Willdawg
07-26-2005, 12:12 AM
A simpler way I guess would be to put a hidden image and onclick popup I dunno just a thought.

Terry36
08-10-2005, 06:15 PM
Rivian...I am an artist and am trying to do the same thing with my pop up windows. I am using Golive to generate my source code (if that is the right terminology). I can insert java script to disable the right click function on the entire page but not on my pop up images. I don't mnd people copying the page with all the thumbnails..but woud like to prevent copying the image in the pop up windows. I tried to insert the script you provided..but don't know anything! Where do I insert it? Thanks a lot for any help!

Terry36
08-14-2005, 03:53 PM
Just bumping this up. I haven't figured out how to disable my right click on the pop up window only. Help anyone?
Thanks,
marion

jihan
08-22-2011, 04:27 AM
Ok, so I answered my own question and for anyone else who comes across this problem, here's the fix.

<script type="text/javascript">

// JK Pop up image viewer script- By JavaScriptKit.com
// Visit JavaScript Kit (http://javascriptkit.com)
// for free JavaScript tutorials and scripts
// This notice must stay intact for use

var popbackground="black" //specify backcolor or background image for pop window
var windowtitle="Larger View" //pop window title

function detectexist(obj){
return (typeof obj !="undefined")
}

function jkpopimage(imgpath, popwidth, popheight, textdescription){

function getpos(){
leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
if (window.opera){
leftpos-=screenLeft
toppos-=screenTop
}
}

getpos()
var winattributes='width='+popwidth+',height='+popheight+',resizable=no,left='+leftpos+',top='+toppos
var bodyattribute=(popbackground.indexOf(".")!=-1)? 'background="'+popbackground+'"' : 'bgcolor="'+popbackground+'"'
if (typeof jkpopwin=="undefined" || jkpopwin.closed)
jkpopwin=window.open("","",winattributes)
else{
getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
jkpopwin.moveTo(leftpos, toppos)
jkpopwin.resizeTo(popwidth, popheight+30)
}
jkpopwin.document.open()
jkpopwin.document.write('<html><title>'+windowtitle+'</title><body '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em" onBlur="self.close()" onClick="self.close()" onContextmenu="return false;"><meta http-equiv="imagetoolbar" content="no"><br>'+textdescription+'</body></html>')
jkpopwin.document.close()
jkpopwin.focus()
}
</script>

The piece that I highlighted in orange is a snippet of code that I added to my script. This is all it took to disable the right click in my pop up ONLY. The rest of my page continues to use right click normally. YAY!!!

I wanted to thank you, John, for helping me. It pushed me to seek out my own solution :) and I really appreciate that!

~Rivian

dear Rivian...
i wanna ask u something that according to your script...
i have problem with disable right click in my program....
i wanna display my pdf file on new windows that if i clicked the link will display on new windows n the right click is disabled..

this is my script :
<td bgcolor="#C0C0C0" width="229" align="center"><A HREF="javascript:void (0)" onclick="window.open('<%=filepath%><%=rs("sistemname")%>#toolbar=0&navpanes=0&statusbar=0','welcome','menubar=no,status=no,location=no,toolbar=no,resizable=yes')">
<font size="2"><%=rs("filename")%></font></td></a>

so what must i do to disable the right click according with my script above...
i really hope your reply for giving me a way out

best regards

jihan