views:

34

answers:

1

Hi , I want to send a mouse event and a variable to a JS function from within my html code, i can send the event fine but the problem is sending the event AND a variable. I have included a stripped down version of what I am trying to do. The browser is chrome. Can anyone offer some advice please ??`

function mouseDown(e,w) {
var ctrlPressed=0;
var evt = navigator.appName=="Netscape" ? e:event;

ctrlPressed =evt.ctrlKey;
self.status="" +"ctrlKey=" +ctrlPressed 

    if (ctrlPressed) 
    alert ("Mouse clicked with Ctrl/n and the variable is " + w )
    return true;
}

</script>
<body>

<table border = "1" >
<tr> 
<td onmousedown="mouseDown(e,\"variable\")"> Link 1 </td>
<td> Link 2 </td> 
</tr> 
</table> 
</body>`
+2  A: 

Don't use browser detection. Check whether an object exists instead.

var evt = navigator.appName=="Netscape" ? e:event; // wrong
var evt = e ? e : event;    // right
var evt = e || event;       // also right

Since the event is passed as the 1st argument, you could try

<td onmousedown="mouseDown(arguments[0], 'variable');"> Link 1 </td>

(I have only tested this on Safari, not guarantee to work on other browsers.)

KennyTM
thanks that did it !!
Mick
If you use `onmousedown="mouseDown(event, 'variable')"`, it'll work in IE and other browsers without any further fixes. This is because the first argument is defined to be called `event` on browsers that support event arguments, whereas on IE the same reference to `event` means the global `window.event`.
bobince
@bobince: Ah thanks. Did not know that.
KennyTM