Page 1 of 1

Context menu and text selection

Posted: Tue Jun 22, 2004 12:43 am
by Gen-ik
Hello there..

I've been searching high and low for a way to do this but haven't had much luck so I hope you guys can help.

I'm after a bit of JavaScript that will prevent text selection and will prevent the context menu being shown (when you right click). This is easy enough to do with IE but I'm having trouble getting it to work with other browsers.

For example with IE the following will work..

Code: Select all

document.onselectstart = function () { return false }
document.oncontextmenu = function () { return false }
I know that won't work with other browsers due to the way events are handled.. so PLEASE HELP GUYS!

Posted: Tue Jun 22, 2004 1:56 am
by Illusionist
have you tried just:

Code: Select all

<body oncontextmenu="false" onselectstart="false">
untested, but i think i've seen it done...

Posted: Tue Jun 22, 2004 2:00 am
by Illusionist
ehh... nevermind! it doesn't work! lol

Posted: Tue Jun 22, 2004 2:06 am
by Illusionist
try this:

Code: Select all

<script>
function disabletext(e)&#123;
return false
&#125;

function reEnable()&#123;
return true
&#125;
document.oncontextmenu = function () &#123;return false&#125;
document.onselectstart = function () &#123;return false&#125;
document.onmousedown   = disabletext
document.onmouseclick  = reEnable
</script>

Posted: Tue Jun 22, 2004 2:23 am
by Gen-ik
Heeeey! Nice one mate, it works fine.. well on IE and NS.. haven't tried it on any thing else because I only have those two browsers installed at the moment.

Thank-you :)

Posted: Tue Jun 22, 2004 2:41 am
by Illusionist
i've tried it on firefox and it works like a charm as well!

Your welcome! 8)