PDA

View Full Version : Help with this JS code



???
05-19-2006, 06:40 PM
Can somebody tell me why it wont work?
<script language="javascript">
document.getElementById('element').style.display = 'none';
function click(e) {
if (navigator.appName == 'Netscape'
&& e.which == 3) {
document.getElementById('element').style.display = '';
}
else {
if (navigator.appName == 'Microsoft Internet Explorer'
&& event.button==2)
document.getElementById('element').style.display = '';
}
}
document.onmousedown=click
</script>
<div id="element">hihihi</div>

Twey
05-19-2006, 06:51 PM
Wow, what an awesomely specific title.
Don't set display to an empty value; instead, use 'block'.
The language attribute is deprecated; the type attribute is required.
<script type="text/javascript">
var z = document.getElementById('element');
z.style.display = 'none';
document.onmousedown = function(e) {
if (navigator.appName == 'Netscape' && e.which == 3)
z.style.display = 'block';
else
if (navigator.appName == 'Microsoft Internet Explorer' && e.button==2)
z.style.display = 'block';
}
</script>
<div id="element">hihihi</div>

???
05-19-2006, 07:02 PM
what does z mean? how does it link to the div, "element"?

???
05-19-2006, 07:06 PM
wait nevermind

???
05-19-2006, 07:09 PM
I dont know why but it still doesnt work...

Twey
05-19-2006, 07:34 PM
This works a lot better.
<script type="text/javascript">
var z = document.getElementById('element');
window.onload = function() {
z.style.display = 'none';
document.onmousedown = document.oncontextmenu = function(e) {
if (navigator.appName == 'Netscape' && e.which == 3)
z.style.display = 'block';
else
if (navigator.appName == 'Microsoft Internet Explorer' && e.button==2)
z.style.display = 'block';
};
};
</script>

???
05-19-2006, 07:48 PM
thanx!
Now im wondering how to make an element aligned with the cursor, i tryed this
var w=1;
while (w==1){
menu.offsetLeft=Mouse.x;
menu.offsetTop=Mouse.y;
}
it doesnt work though

Twey
05-19-2006, 07:58 PM
I take it you're trying to create something like this:
<script type="text/javascript">
var z;
function fakeMouseEvent(x,y) {
this.pageX = x;
this.pageY = y;
}
window.onload = function() {
z = document.getElementById("element");
document.onmousemove = function(e) {
z.style.top = ((e.clientY ? e.clientY : e.pageY) + 30) + "px";
z.style.left = ((e.clientX ? e.clientX : e.pageX) + 30) + "px";
};
document.onmousedown = document.oncontextmenu = function(e) {
if (e.which && e.which == 3)
z.style.display = 'block';
else if (e.button && e.button == 2)
z.style.display = 'block';
};
};
</script>
<div id="element" style="display:none;position:absolute;">hihihi</div>?
/EDIT: This one's IE-compatible. :)

???
05-19-2006, 09:07 PM
Is there a way to easily stop and start that code?

Twey
05-19-2006, 09:10 PM
No, but you can just set display:none again on the element. There won't be a serious performance hit.

???
05-19-2006, 09:11 PM
Oh Du

mwinter
05-20-2006, 09:54 AM
Don't set display to an empty value; instead, use 'block'.In this specific case, that advice is fine as div elements are 'pure' block-level elements. However, in the general case, the empty string is exactly the value to use.

The code is still broken: no feature detection, uses browser detection, and assumes that IE passes the event object as an argument (which it only does after using the attachEvent method).



var global = this,
element;

global.onload = function() {
if (document.getElementById
&& (element = document.getElementById('element'))
&& element.style) {
element.style.display = 'none';

document.onmousedown = function(e) {
if ((e = e || global.event)
&& (((typeof e.button == 'number') && (e.button == 2))
|| ((typeof e.which == 'number') && (e.which == 3)))) {
element.style.display = '';
}
};
}
};
global.onunload = function() {
element = null;
};
Mike