[SOLVED] Context menu and text selection

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Context menu and text selection

Post 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!
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

have you tried just:

Code: Select all

<body oncontextmenu="false" onselectstart="false">
untested, but i think i've seen it done...
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

ehh... nevermind! it doesn't work! lol
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 :)
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

i've tried it on firefox and it works like a charm as well!

Your welcome! 8)
Post Reply