Forum Moderators: open

Message Too Old, No Replies

Call function ONLY when needed?

Script works, but I only want it to when I call it.

         

JAB Creations

9:54 pm on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This script works but I want to have it do so only when I call the function. How do I apply that to this script?

function oncontextmenu(event) {alert("disabled"); return false;}

John

Rambo Tribble

2:42 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what you mean by 'when I call the function'. The event is out of your control; only the response can be modified. You could set a flag to determine execution context.

JAB Creations

3:33 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I only want to apply this to specific links...
<a href="#" onclick="preview();">link</a>

How do I mod the function to execute only when I call it?

John

Bernard Marx

3:56 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi John,

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>

JAB Creations

4:48 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



***EDIT***
Yes Bernard! Thats EXACTLY what I'm after! An alert and no contextmenu but only on the anchor I call the function with. How can we do that universally?

*/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

Bernard Marx

4:52 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds complicated, John.

Could you explain what you want to do, in terms of "When the link is right-clicked .., except for when .., instead ...".

JAB Creations

4:57 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok...

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!

Bernard Marx

6:47 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the least fuss method.
I'm afraid Opera just won't play ball.

<script>
function preview(e)
{
alert("prev");
}

function contextMenu(e)
{
alert("contextMenu");
}

</script>

<a href="#"
onclick="preview(event);"
oncontextmenu="contextMenu();return false;"
>
Test</a>

JAB Creations

7:52 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoa! Rock on Jude! 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...

Bernard Marx

9:01 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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>

Rambo Tribble

2:17 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mr. Marx,

Is there a reason you chose a regular expression instead of a string method to test for the class name (I mean, besides the ineffable elegance of your inimitable style)?

Bernard Marx

8:48 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The purpose of the regular expression, Mr. Tribble, is to afford Mr. Creations the opportunity to add further class names to the elements for styling purposes, if he so wishes.

Rambo Tribble

2:09 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would not the use of indexOf afford such possibilities, as well?

JAB Creations

7:23 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am assuming this is the regular expression?

(!/noRClick/.test(src.className))

I've found other things I've had an interest in learning require require some experimenting with expressions.

Mr. Marx is a gentleman I hold in high regards for his overwhelming scripting abilities. :)

Bernard Marx

9:17 am on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mr Tribble has a few up his sleeve too. He does have a point.

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, ...