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);
 
the beauty of using a form with the path/filename is the browser does the work of handling spaces and stuff without you having to wory about it ;-)

so 'this isn't a great file name' doesn't matter as you have it in a form field only double quotes would break it programaticaly :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Well considering my most recent efforts of tinkering left the table row per filename like
Code:
<tr style="font:14px Arial"
onmouseover="bgColor='8181CC'; style.cursor='pointer'; return true"
onmouseout="bgColor=''; return true"
oncontextmenu="window.location='index.php?path=this is a path&fname=this isn\' a great filename.txt'; return false">
How would I get from that to something checking which mousebutton I pressed and if lmb going straight ot the file and if rmb going to the php page, with or without a form? Sorry it's late I'm tired and I've spent so many hours trying to pass things around in js/php and still had that open/save box laugh at me with it's _ spaces etc =_=

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
ok, lets have a go using kaht's method.
Code:
<html>
<head>
<script type="text/javascript">

function mouseInfo(fnum) {

   var e = window.event;

   if (e.which) {
      if(e.which == 2)
        { 
            e.returnValue=false;
            document.getElementById('form'+fnum).submit();
            
        };
   }
   else if (e.button) {
      if(e.button == 2)
        {  
            e.returnValue=false;
            document.getElementById('form'+fnum).submit();
            
        };
   }

}
</script>
</head>
<body>
<table border="1">
    <tr>
        <td onmousedown="mouseInfo('1');">Click Here
            <form id="form1" action="[URL unfurl="true"]http://www.domain.com/yourfile.php">[/URL]
                <input type="hidden" name="fname" value="this is a bad file name">
                <input type="hidden" name="fpath" value="this/is/the/path">
            </form>
        </td>
    </tr>
</table>
</body>
</html>

obviously the form id needs to be generated dynamically along with your table data and the relevant onmousedown call to the script coresponding to the form ID.

However, although this test works, I don't seem to be able to stop the menu poping up, which is a bit of a pain, maybe someone else here can help on that one!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
OK I've had another play, as I found the code didn't work for Firefox, because FF doesn't handle mousedown with either of those methods, .button or .which , but I have found that FF likes oncontextmenu.

So to make it work in FF and IE simply use the following code, it also eliminates the popup menu and i've added the method="post" which I forgot on the previous form...

P.S. it doesn't work for Opera, hey ho!
Code:
<html>
<head>
<script type="text/javascript">
function mouseInfo(fnum) {

    document.getElementById('form'+fnum).submit();

}
</script>
</head>
<body >
<table border="1">
    <tr>
        <td oncontextmenu="mouseInfo('1'); return false;">Click Here
            <form id="form1" action="[URL unfurl="true"]http://www.domain.com/yourfile.php"[/URL] method="post">
                <input type="hidden" name="fname" value="this is a bad file name">
                <input type="hidden" name="fpath" value="this/is/the/path">
            </form>
        </td>
    </tr>
</table>
</body>
</html>

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
OK thanks, I tried your method there and I've got it working. In theory though this is now the same as my original onclick and oncontextmenu way of doing things, but with a lot more html in order to get the js function and forms going. The result however is the same unfortunately. I've been discussing it in the php forum to no avail. Whether it's php, or my xpsp2 or ie7, something somewhere loves _'s :(


_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
have you tried URL escaping the filename before facilitating the download? what happens if you hardcode 'my%20file%20name' to test, does that work?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
ok so I told php to swap " " for "%20" in the variable it had got from the js (via GET or via POST and your form idea) and ... lo and behold it works!! It shows exactly how it should be, in both ie and ff.

I KNEW I'd managed to turn _ into spaces at 1 point, thanks for prompting me indirectly and sticking with this :)

It seems the moral of thes tory is, the open/save dialog box needs filenames passed to it (however you do it in js/php etc) with spaces turned to %20 else it turns them to _'s. You can fix your dj-c.d.c.mp3 now ;)

I'm not sure if I'll stick with all the forms or jsut go back to an onclick and oncontextmenu if that seems to work on most browsers?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
In PERL I have to use the the URI::Escape module.

But that isn't the issue with my filename problem it's because of the dots!

dj-c.d.c..mp3 - you see the double dot's which really screws things up.

Glad you sorted your problem, though I thought using the form solved your apostaphe (') problem in file names, you will need to escape that also else I assume your js call will break
Code:
mouseInfo('there's an apostophe in this file name','path');




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I'm not using a jscall and forms anymore since it results no differently to just using an onclick and a oncontextmenu with addslashes() in php applied to the value.

As it happens I hadn't quite solved my problem. In FF it then came up as "file%20name" etc. So I now have it check if it's IE, and only encode it for that

Code:
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
	$fpath = rawurlencode($fpath);
	$fname = rawurlencode($fname);
}
Yes I know that's php ;P Just sharing my final results.
My final js/html looks like this (with php variables put in and "'s un\escaped for this paste)
Code:
<tr	onmouseover="bgColor='8181CC'; style.cursor='pointer'; return true"
onmouseout="bgColor=''; return true"
onclick="window.location='".addslashes($filename)."'"
oncontextmenu="window.location='index.php?fpath=".addslashes($subdir)."&fname=".addslashes($showname)."'; event.returnvalue=false; return false;">

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
cool, don't know the addslashes as i'm a perl head, glad you're all sorted - have a star!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Thanks!

addslashes() - Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).

Similar to

htmlspecialchars() - The translations performed are:
'&' (ampersand) becomes '&amp;'
'"' (double quote) becomes '&quot;'
''' (single quote) becomes '&#039;'
'<' (less than) becomes '&lt;'
'>' (greater than) becomes '&gt;'


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

Part and Inventory Search

Sponsor

Back
Top