Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catchall rightclicks 4

Status
Not open for further replies.

Leozack

MIS
Oct 25, 2002
867
0
0
GB
Hi all - just wondering what the shortest way to catch all rightclicks is, pref crossbrowser, and pref only for certain elements?

I have a bunch of table cells acting as onmouseover/onmouseout/onclick links, but I want to give people the option of rightclicking them to save rather than leftclicking them to window.location to them (which in the case of mp3's or images etc views instead of tries to download, and I can't change that user+browser behaviour). I can't think of another way round this and I'm already using php to return headers to force a download for the potential rightclick code.

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
there is no true cross browser way, but for IE you can put this in the body tag
Code:
onContextMenu="call_my_script();"
remember some Macs don't even have a right mouse button.

So for all those who claim you should not use target when doctype = X/HTML Strict - as people can use right click - open in new window - how do you do that when there isn't a right mouse button?





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
On a MAC yo use option+click I believe to get such a menu ;)
Thanks for the IE catchall, any ideas for operating on rightclicks for just an element like a link? I saw in some code an "onrightclick" the same as you have a "onclick" event, but that hasn't worked for me. I'd like it to work in firefox too if preferable.

I can't think of any other way to give users a choice of viewing or downloading from a link without it being a normal href link so they can choose themself which is more ugly than nice table cell links (which aren't rightclickable as links without an onrightclick event) :(

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
you could put the oncontextmenu in the td tag i guess and use it something like this
Code:
 <td oncontextmenu="my_script('link info')">
not sure if it will work, but you could always use ondblclick and have a script to produce a menu with double click.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Ahh good thinking with the td context menu. Works a treat with a return false on it!

Though thanks to security in xpsp2 when I rightclick and my php page sends headers to download a file, I get the infobar up the top saying download has been blocked click here to allow it >_> There's always something

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
aha Dan what are the codes for the buttons and how do you query the event?

I take it some variable is set to a value which can be queried to work out what was clicked?

Leozack -> I do a similar thing with PERL, the way i get round it is to use a href pointing to a perl script, the script authenticates request and then prints headers and binary direct to browser, this pops up the standard "Open/Save" dialog box, instead of actually changing the current page URL.

that might work for you, but you will need to handle the request to your PHP program correctly, I only know how to do this with PERL, but can at least give you some pointers :)



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
My PHP page does that. I point back to the current page but add on some variables and it takes those and uses them as header info to push out the download. Currently the only problem is having _'s instead of spaces in the filename it wants to save as, and a filename like "this isn't a great filename" wants to save as "this_isn_'t_a_great_filename" -_-

The mouse button code is event.button, and left=1, right=2, middle=4 if I'm not mistaken. Though currently I'm struggling to use this instead of oncontextmenu because I can't find a way to check what button was pressed and then move the window location, since there is problems sending the file path and filename to a function that checks what button was clicked before deciding how to window.location onwards either directly or via the php page for download headers.

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
odd, I typed out a response and never pressed submit apparently.....

Anyway, 1DMF - this code will show you how mouse captures are handled in each browser and what code corresponds to each browser:
Code:
<script type="text/javascript">

document.onmousedown = mouseInfo;

function mouseInfo(e) {
   e = (!e) ? event : e;
   if (e.button) {
      alert("using button\n\nbutton clicked: " + e.button);
   }
   else if (e.which) {
      alert("using which\n\nbutton clicked: " + e.which);
   }
   else {
      alert("I don't know what you support");
   }
}

</script>

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Yeah I have a problem with filenames on my download as the artist name is dj-c.d.c. and so it wants to save as dj-c.d.[1].c.mp3 - but once the dialog box to save is clicked the user can change the filename nayhow - so I don't see it as a major issue, as long as the functionality works, the filename is a minor irritance

as for the stuff on events, thanks guys (and gals) great info.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Actually, I was just checking my code and found something interesting.

The event.button exists in firefox for the right click and middle click, but does not for the left mouse button. So, the order of the if statements should be changed:
Code:
<script type="text/javascript">

document.onmousedown = mouseInfo;

function mouseInfo(e) {
   e = (!e) ? event : e;
   if (e.which) {
      alert("using which\n\nbutton clicked: " + e.which);
   }
   else if (e.button) {
      alert("using button\n\nbutton clicked: " + e.button);
   }
   else {
      alert("I don't know what you support");
   }
}

</script>

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Thanks 1DMF for the star, that was #400 [thumbsup2]

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
you're always worth a star in my book kaht [angel]

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Though currently I'm struggling to use this instead of oncontextmenu because I can't find a way to check what button was pressed and then move the window location, since there is problems sending the file path and filename to a function that checks what button was clicked before deciding how to window.location onwards either directly or via the php page for download headers.

could you check what button was pressed and then use either
Code:
 document.location.href
for left click and set a form up for right click and use
Code:
document.formname.submit();

and if you wan't to be 100% correct i beleive you should use window. not document.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Well the problem is my onmouse function sitting atop the page in js script, needs to be called with event.button, and take appropriate action. But if the action is different for every filename clicked, I have to pass the filename and path to the funciton and use the function to redirect the window. This is where things start breaking as you have to have the filenames written out in the page as part of the call to the onmouse checking function. Encoding and things are causing problems.

The leftclicking works just fine and always did. The code for rightclicking is working fine now with oncontextmenu but I can't get the filename to display right. That's in IE7, in FF apparently you get the first block of text from the filename then that's it soon as you hit a space. I'd be interested in this form method you're referring to since I can't think of how you'd use that to download a file when they rightclick an obejct.

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
I guess you are populating the onclick via the PHP for the relevant filename and path, can you not URL Escape this before placing in the onclick js call.

or a long winded way with forms .....

this would be build dynamically like the table content and put a form in each <td> (note: [$a] represents the dynamic loop counter eg 1,2,3,4 etc...)
Code:
<form name="form[$a]" action="yourphp code">
<input type = "hidden" name="file" value="path and filename">
< you could add any post vars and values you want here dynamically >
</form>
[code]
then the onclick can be set to to call your event routine passing it the value of [$a] of the form needing submitting

i.e..
<td onclick="my_script([$a]);">

and the script checks for event button (right click)

function my_script(formnum){

do checking for right button then 
document.form+formnum.submit();

not sure on syntax here you might need to do an eval on it or something JS isn't my strong point, I just get by :-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Sounds fiddly but possible, just not sure hwo to use a form to get a file downloading to the client e.e

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
I thought the PHP facilitated the download and that's where the form submits too, and contains all the post vars required to identify which file is required.


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
ah I see make the form's target the address I'm currently making the window goto. That way I can transmit the variables in POST not GET. But hmm they'd still need to be written in plaintext in the outputted html in the form eg <input type="hidden" name="fname" value="This isn't a great filename"> ... but maybe I can get away with that. Worth a try. But I'm still not seeing the ability to use onclick and check what event.button is as possible whereas using onclick for left and oncontextmenu for right works fine. But I guess maybe not in FF.

But so far if I have a cell with anonclick, I can't for the life of me work out how to call a function to do all that checking and work without passing it the path and filename which is what I was trying to avoid? :/

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top