Forum Moderators: open

Message Too Old, No Replies

return false not stopping context menu in IE/Firefox?

Script also just plain not working in Opera 8.5

         

JAB Creations

7:40 am on Dec 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These two JavaScript functions are intended to stop people from doing zanny things like seeing a blank page with a Quick Time plugin because they had to preview a download link and tried to download a preview link (I've seen countless examples of this in my access log sadly).

Anyway download links are intended to be right click (save link as/save target as). Preview links are intended to be left clicked.

<script type="text/javascript">
function download() {alert("This is a download link, right click to save the file!"); return false;}
function preview() {alert("This is a preview link, left click to preview the song!"); return false;}
</script>

<a class="download" href="somefile.mp3" onclick="download();return false;">Download</a>

<a class="music" href="preview.php" onmouseup="preview();return false;" rel="mplayer" title="Play the high quality preview of this song.">Preview</a>

Now here are the problems I face...

1.) Firefox/IE will STILL show the context menu after the alert reguardless if I return false (twice even). How do I stop the context menu from coming up then?

2.) Opera just is not stopping the context menu on the preview links OR setting an alert.

What DOES work is when you left click a download link none of the three attempt to download the file (thankfully).

John

koppted

4:55 am on Dec 30, 2005 (gmt 0)

10+ Year Member



use this code


<script language=JavaScript>
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers¦¦(document.getElementById&&!document.all)) {
if (e.which==2¦¦e.which==3) {(message);return false;}}}
if (document.layers)
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
</script>

ajkimoto

6:50 pm on Dec 30, 2005 (gmt 0)

10+ Year Member



JAB Creations,

I think what might be happening is that the contextmenu event is firing just before the mouseup event.

In any event (no pun intended), mouseup will fire whether they right or left click, so the existing code won't do what you want anyway.

Try using the oncontextmenu event handler instead to handle the right-click. This should give you what you want in IE and FF/Moz:

<a href="myaudio.mp3" oncontextmenu="preview();return false;">Preview</a>

Note: I seem to remember that oncontextmenu will cause the page to not validate as XHTML Transitional, but this will not affect performance.

Hope this helps,

ajkimoto