Forum Moderators: open
function oncontextmenu(event) {alert("disabled"); return false;}
Shouldn't that be:
document.oncontextmenu = function(){ ... }
How do I mod the function to execute only when I call it?
Erm. Isn't that how functions always behave?
------------------------------------------
Is this what you're after (NB: This version IE-only)
<script>
document.oncontextmenu = function()
{
if(!/\bpreview\b/.test(event.srcElement.className))
return true;
alert("disabled");
return false;
}
</script><a href="#" onclick="preview();" class="preview">Test</a>
*/edit (didn't work in Firefox and overlooked your mentioning of IE. /*
I'm still trying to get this to work amazingly IoI...
function preview()
{
if (document.oncontextmenu)
{return false; alert("disabled");}
}
Still not working...
I have another idea now that I'm aware of document.onmouseover...
We start with document.onmouseover and then look for a function on the element. If the script sees the function then it returns false on the contextmenu and alerts the user. Could it be done that way?
John
User right clicks anchor that calls function, script executes, cancels contextmenu, alerts user.
User right clicks any other anchor or element, script is NOT executed and contextmenu works normal.
People are right clicking and saving previews which are static HTML pages and then left clicking and playing plugins within the frame (breaking navigation).
This script is to keep people from saving links that they (for who knows what reason) believe to be mp3s and to alert them about it.
Hopefully I've explained it just right? Thanks!
Not to look a gift-horse in the mouth (or whatever the saying is) but it does not work in Opera 7/8/9prev.? I'm sure it's some kind of DOM issue, any idea how we can approach it?
Does (e) specify the event of the contextmenu?
John
PS */ I think the alert is also happening on normal left clicks which I don't want it to. How do we ensure this only alerts on right click? /*
PS/PS IoI...
I just noticed you used the oncontextmenu attribute which will not validate. This script feels impossible...
it does not work in Opera 7/8/9prev.? I'm sure it's some kind of DOM issue, any idea how we can approach it?
The Opera people are all puffed up on some moral tip where oncontextmenu is concerned. We could just ignore it.
Click this [google.com] if you need help visualising the possible mess.
Does (e) specify the event of the contextmenu?
Yes. Both in IE and Moz
- but for slightly different reasons. The event object is not passed by IE in the code below, for instance.
I think the alert is also happening on normal left clicks which I don't want it to.
I can't commit to this, but it may have something to do with there also being an onclick handler present on the element.
I just noticed you used the oncontextmenu attribute which will not validate.
Ignorance, again, is bliss. Personally, I'd just ignore the validator's message about that particular attribute. Browsers themselves don't validate, and certainly don't care about invalid attributes. The validator is a pattern of electrical charges stored on a machine somewhere. You are a human with goals (I realise that this contradicts your deterministic views).
However, in order to please you to the best of my abilities, I offer you this one. It takes advantage of event bubbling so as to avoid having to do all that "onload > loopy-loo".
..and, Opera? No.
/* change corrupted [b][red]¦¦[/red][/b] chars for pipes */
/**/
function getEventSrc(e)
{
if(this.Event)
{
var targ = e.target;
return targ.nodeType==1? targ : targ.parentNode;
}
else
return event.srcElement;
}function preventDefault(e)
{
e=e¦¦event;
e.preventDefault? e.preventDefault() : e.returnValue = false;
}document.oncontextmenu = function(e)
{
var src = getEventSrc(e);
if(!/noRClick/.test(src.className)) return;
preventDefault(e);
alert("contextMenu");
}</script>
<a href="#" class="noRClick">Test</a>
The idea is to use a RegExp to test the className instead of using
src.className == "noRClick"; which would mean that no further classes could be added, or the script would fail.
Initially, I started with
/\bnoRClick\b/.test(src.className) This would test for that exact "word". I removed the word-boundaries (\b), because I considered that it was unlikely that any other class would contain that string anyway. However, with those specifics removed, we just end up with a plain test for the presence of the string, which Tribble points out to be more efficiently done using indexOf. No creation of a regexp required.
--------------------
To answer your question, only the contents of the forward slashes regular expression (a RegExp literal denoted by the /.../). The regexp uses its inbuilt method, test, on the element's class name.
I'd would certainly recommend giving regular expressions a little study. Just the basics will make your browser scripting more convenient, but that's not all. Javascript uses Posix style regular expressions; if you get the hang of them here, you'll have a head start when it comes to using them in all other environments that support them
- Unix script, PHP, SQL, your text editor, ...