PDA

View Full Version : [DHTML] No Right Click



cr0w
04-19-2007, 10:32 PM
1) CODE TITLE: No Right Click

2) AUTHOR NAME/NOTES: Cr0w

3) DESCRIPTION: I keep seeing all these 100 line long no right click codes, and they always annoy me since it can be achieved in one line. So here it is.

4) This code is so short that i saw no reason to attach it/remotely host it:

<script type="text/javascript">
<!--
document.oncontextmenu=function anonymous(){ return false; }
//-->
</script>

pcbrainbuster
04-19-2007, 10:40 PM
It can get smaller... -

<script>
<!--
document.oncontextmenu=function() {return false}
//-->
</script>

There is a slight difference...

cr0w
04-19-2007, 10:46 PM
function() isn't proper javascript...you've gotta add the "anonymous"

cooldude42192
04-19-2007, 11:33 PM
Doesn't work in Opera 9. And rather useless IMO, we all know this isn't going to stop anyone determined ;)

cr0w
04-19-2007, 11:49 PM
And rather useless IMO, we all know this isn't going to stop anyone ;)

And the other 100-line-long ones already in Dynamic Drive's database are going to stop anyone?

cooldude42192
04-19-2007, 11:50 PM
I didn't say they would... but the point was these scripts aren't a good "security" measure.

mwinter
04-20-2007, 01:00 AM
function() isn't proper javascript...you've gotta add the "anonymous"

Not at all. The function keyword is associated with two constructs: function declarations, and function expressions. With the latter, the identifier following the keyword is optional. Moreover, its scope is different: the identifier is only accessible within the body of the defined function[1].

Any decent user agent will give users the option to override any attempts to disable the context menu, so this sort of script is guaranteed to fail for some percentage of visitors. Regardless, the context menu is an important feature and shouldn't be disabled or replaced.

Mike


[1] There are implementation discrepancies among some browsers, but that is how function expressions are supposed to work. Depending on other behaviour is not recommended.

tech_support
04-20-2007, 08:46 AM
The no-right click script is a joke. What if someone wants to copy for their homework? What if someone has JavaScript disabled?

Twey
04-20-2007, 10:04 AM
<script>
<!--
document.oncontextmenu=function() {return false}
//-->
</script>The type attribute is required, the HTML comment tags aren't (and will cause the whole script to be ignored in XHTML). Also, I think undefined should be interpreted in the same manner as false. Thus, the smallest possible variation in HTML 4.01 Strict would become:
<script type=text/javascript>document.oncontextmenu=function(){}</script>XHTML requires the quotes around the type attribute.

tech_support
04-20-2007, 10:05 AM
Why return 0?

Twey
04-20-2007, 10:06 AM
Because it's shorter than false. Also, I've just realised that if 0 will work undefined will too, so it's not necessary to return anything, and edited accordingly.

mwinter
04-20-2007, 10:30 AM
XHTML requires the quotes around the type attribute.

So does HTML.



Also, I've just realised that if 0 will work undefined will too, so it's not necessary to return anything, and edited accordingly.

The value undefined is usually a special case though for event listeners. Ignoring it, or treating it like boolean true, allows authors to write event listeners without having to always return a value. I haven't checked if this applies here.

Mike

Twey
04-20-2007, 10:31 AM
So does HTML.Truly? The validator says otherwise: a bug?
The value undefined is usually a special case though for event listeners. Ignoring it, or treating it like boolean true, allows authors to write event listeners without having to always return a value. I haven't checked if this applies here.Hm, best return 0 then, I suppose.

mburt
04-20-2007, 10:38 AM
Does null have the same value as 0 or false?

mwinter
04-20-2007, 11:00 AM
Truly?

Yes. What you posted is technically equivalent to (line breaks added for clarity):



<script type="text">
javascript&gt;document.oncontextmenu=function(){}&lt;
</script>
script&gt;

Of course, no browser will see it that way, but the validator does. If only the parse tree option was still available...



The validator says otherwise: a bug?

Doubtful. Can you post the snippet of code you used?



Does null have the same value as 0 or false?

No. It's its own value and type. If you meant, "Does it type-convert to boolean false like 0 and false?", then yes.

Mike

Twey
04-20-2007, 11:10 AM
The precise code was:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<script type=text/javascript>document.oncontextmenu=function(){return 0}</script>
</head>
<body><p></body>
</html>

mwinter
04-20-2007, 12:17 PM
Yes. What you posted is technically equivalent to (line breaks added for clarity):



<script type="text">
javascript&gt;document.oncontextmenu=function(){}&lt;
</script>
script&gt;


I made an incorrect assumption: a NET-enabled start-tag doesn't need to be closed with a NET; any end-tag will do.

The "real" equivalence is:



<script type="text">
javascript&gt;document.oncontextmenu=function(){}
</script>

As you can see, this is valid, but not "correct".

Mike

cooldude42192
04-20-2007, 01:27 PM
Sorry to spam this very interesting discussion (no sarcasm intentended), but why are we talking about ways to shorten a useless script? :p

Twey
04-20-2007, 01:33 PM
Academic curiosity? :)

cooldude42192
04-20-2007, 01:40 PM
Academic curiosity? :)

Works for me :D Just out of curiosity, any way to get this script working in Opera? Not that I'd ever put it on a page, but in the interest of "academic curiosity"...

Twey
04-20-2007, 01:42 PM
Certainly, untick the option to disallow scripts from replacing the context menu :) If there were any other way, it would be a bug in Opera.

cooldude42192
04-20-2007, 01:44 PM
Ah, that would do it :p I don't use Opera much, so I wouldn't know.

cr0w
04-21-2007, 02:37 PM
Obviously i know how useless this code is, but if it truly is such a waste of time, why are there about 4 of them in Dynamic Drive's database?

Twey
04-21-2007, 03:18 PM
Oh, there are worse in DD's database -- they're kept purely in order to avoid breaking links, apparently. We've suggested archiving them with a clear notice that they're no longer supported and shouldn't be used, don't know what the verdict is on this yet though.

jscheuer1
04-21-2007, 04:06 PM
Need more right click scripts, must have right click scripts.

mburt
04-21-2007, 04:44 PM
Oh, there are worse in DD's database
"there" definitely worse... Twey made a grammatical error and I didn't. Wait. How do you spell grammatical, is that right? :p

Twey
04-21-2007, 05:36 PM
Yes, "grammatical" is right, but there are no errors in that post that I can see... I said there are worse, not "there worse" as a phonetic confusion of "they're."

tech_support
04-22-2007, 08:41 AM
The precise code was:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<script type=text/javascript>document.oncontextmenu=function(){return 0}</script>
</head>
<body><p></body>
</html>
Isn't the text/javascript suppose to be "text/javascript"?

mwinter
04-22-2007, 11:11 AM
Isn't the text/javascript suppose to be "text/javascript"?
Uh, yeah. We covered that already. :)

Mike

cr0w
04-30-2007, 02:13 AM
Oh, there are worse in DD's database -- they're kept purely in order to avoid breaking links, apparently. We've suggested archiving them with a clear notice that they're no longer supported and shouldn't be used, don't know what the verdict is on this yet though.
o.O DynamicDrive isn't a CMS? You enter it all manually? o.O

Not too smart for such a large script database...

Twey
04-30-2007, 10:11 AM
Haha, we don't, ddadmin does :p I believe the CSS library is based on some form of CMS, but the rest hasn't been updated yet.

cr0w
05-02-2007, 10:50 PM
Haha, we don't, ddadmin does :p I believe the CSS library is based on some form of CMS, but the rest hasn't been updated yet.

Wow, i feel bad for him :p

muffinzrock
05-11-2007, 12:10 AM
hi! im new here- ive been coding webpages for about a year now. i hope to help people on this site.

...heres some HTML i made that blocks right-clicks, dragging, and selecting. Not sure if it works in firefox, but it works in IE:

<body oncontextmenu="return false" ondragstart="return false" onselectstart="return false">


or if ya wanna drive someone crazy but not allowing regular clicks too, heres code for that ;) :
<body onclick="return false" oncontextmenu="return false" ondragstart="return false" onselectstart="return false">

mburt
05-11-2007, 12:41 AM
No offense intended, but: that doesn't work, and maybe in IE. JavaScript can be disabled. Blah blah. This thread should be either closed, or moved to the lounge.

jscheuer1
05-11-2007, 04:10 AM
OK, I'll lock it. If folks want to ramble on about right click, they can do it somewhere else other than in the submit section.