Forum Moderators: open

Message Too Old, No Replies

Return false not returning false!

Left click on mp3 still (unwanted) but works and invalidates preview links!

         

JAB Creations

7:15 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My site incorporates a music player with some songs allowed by featured artists to allow mp3 downloads of their song. There are HiFi and LoFi preview links that I do not want to allow right click and download links that I do not want to allow left clicks.

If the user left clicks the download link it will open the mp3 (usually in a blank page with quick time) which is retarded. I want to force the people to use the links correctly (it's not REALLY that difficult). The same thing with the preview links. You left click to preview and right click to download.

However the script I have made only half works. It will alert the user they are umm, doing the wrong action. However it will still execute their original left/right click which EVER after I had thrown in a return false.

How do I get this to work correctly in the behavior I am hoping for?

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

JAB Creations

7:56 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah ha! Answered another one of my own questions!

<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 href=#" onclick="download();return false;">Download</a>

<a href=#" OnContextMenu="preview();return false;">Preview</a>

Bernard Marx

9:19 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could do:

onclick="return someFunction()
"

..with the

return false
inside
someFunction
.

There. We've save about 6 chars in the event handler. There is another way, involving the cancellation of the default event, but I won't go into it.

JAB Creations

10:45 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gah, I must attend school!

I was half expecting someone to point out the fact that oncontextmenu attribute is not valid on anchors when run through the W3C valiudator.

I haven't had enough time (due to annoying homework) but here is the file I was messing with to find a way around the problem. BM - note I just read your post and will work with what you mentioned when I return from class in...ug -four hours. :(

Anyhu here is my file I was messing with earlier!

<html>
<head>
<script type="text/javascript">
<!--
function OpenLink(theURL) {

window.open(theURL);

document.oncontextmenu = newFunction("returnfalse")

}
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>

</head>

<body>

...which I call like this:

<a href="javascript:OpenLink('http://www.webmasterworld.com.com/')">Site Name</a>

oncontextmenu = newFunction("returnfalse")

<a href="javascript:OpenLink('http://www.webmasterworld.com.com/'); oncontextmenu=newFunction('returnfalse')">Preview</a>
<a href="javascript:OpenLink('http://www.webmasterworld.com.com/'); onclick=newFunction('returnfalse')">Preview</a>

</body>
</html>

Remember I'm just trying to get the oncontextmenu to work without using it as an attribute on the anchor! I figured using a javascript link would be able to achieve this though I have never really tried something like this to the extent you see.

John

Bernard Marx

11:39 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gah, I must attend school!

It's not such a big deal (those 6 chars). I just mentioned it out of interest.

Bernard Marx

11:52 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1)
onclick="[red]newFunction('returnfalse')[/red]"

Hmm. Not in an HTML event handler.

2) You have a quoting problem - on both links. The the oncontextmenu is enveloped inside the href.

How about eg:

<a href="javascript:void 0" onclick="OpenLink('http://www.webmasterworld.com.com/');">Preview</a>