109

I'm using javascript to disable text selection on my webiste.

The code is:

<script type="text/JavaScript">

function disableselect(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function ("return false")

if (window.sidebar) {
  document.onmousedown = disableselect
  document.onclick = reEnable
}
</script>

Similar script can be found here

On my localhost: All Browsers (Firefox, Chrome, IE and Safari) work great.

On my Live site: All ok EXCEPT Firefox.

My questions are:

  1. Does anyone have a suggestion as to why Firefox behaves differently for the live site and local host. Note: Javascript is enabled.

  2. Maybe my script is too simplistic so I've tried the following with EXACTLY SAME Results

6
  • 4
    Let me suggest you to avoid the usage of Javascript for this case. Just use CSS, you can find more information about text-selection in CSS at this question Commented May 29, 2013 at 4:49
  • The second script likely is too old. Remove the !document.all since I believe it breaks the fx support Commented May 29, 2013 at 4:49
  • 15
    I hate sites doing this. If I want to copy some text I can open up dev tools and copy it anyways. This is just an unnecessary inconvenience. Commented Jun 10, 2016 at 20:56
  • 5
    Exactly as Reek says. Don't do this unless you have a very good reason for it. Many people might just want to select something to search for it or use a dictionary to look up a word. Prohibiting this is just an annoyance which might drive people away from your site in long run. Commented May 2, 2017 at 9:36
  • 3
    Firstly, I also hate Pages that forbid selecting (text and whatnot) just for the "fun" of it, but I think it's a good question, as there are some legitimate reasons for one wanting to disable it (e.g., to implement mouse gestures or drag&drop of blocks of text in a game of whatever). Of course, using JS to achieve that end is bad, but the CSS solution also has its problems (in some browsers, user-select alone won't work, it requires the "-vendor-" prefix, like -moz-user-select and -webkit-user-select, so, to make it work, it's necessary to set all possible variations to none). Commented Oct 27, 2018 at 0:33

6 Answers 6

262

Just use this css method:

body{
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

You can find the same answer here: How to disable text selection highlighting using CSS?

Sign up to request clarification or add additional context in comments.

7 Comments

Also add the link from where you found this answer: stackoverflow.com/questions/826782/…
Thank you very much. The CSS in body worked for all cases except IE. Is there neat CSS solution to also solve this problem for IE? I'm now using the combination of Js and CSS to cover all browsers!! thk u.
1. Why hasn't this asnwer been tagged as the solution?... It resolves the issue in question in the most accurate and simple way. 2. It should be mentioned that this CSS defition can be implemented on any HTML element and not only BODY.
@TheCuBeMan "user-select" is a non standard feature, and IMO, is therefore not the best solution.
It probably is the best approach - browsers handle it differently because it's complicated and they all have different takes. Namely, they want to provide the intended UX (no selection) while still allowing the user to select if they really want to. The W3C draft has more notes, and caniuse shows fantastic support. It's okay that browsers handle it differently - the user will get the experience that their preferred browser provides.
|
39

I'm writing slider ui control to provide drag feature, this is my way to prevent content from selecting when user is dragging:

function disableSelect(event) {
    event.preventDefault();
}

function startDrag(event) {
    window.addEventListener('mouseup', onDragEnd);
    window.addEventListener('selectstart', disableSelect);
    // ... my other code
}

function onDragEnd() {
    window.removeEventListener('mouseup', onDragEnd);
    window.removeEventListener('selectstart', disableSelect);
    // ... my other code
}

bind startDrag on your dom:

<button onmousedown="startDrag">...</button>

If you want to statically disable text select on all element, execute the code when elements are loaded:

window.addEventListener('selectstart', function(e){ e.preventDefault(); });

      

1 Comment

This is the best solution, because the CSS solution does not work for divs with the contenteditable attribute (like if you want to disable text selection while the control is being resized by the user's mouse).
11

For JavaScript use this function:

function disableselect(e) {return false}
document.onselectstart = new Function (return false)
document.onmousedown = disableselect

to enable the selection use this:

function reEnable() {return true}

and use this function anywhere you want:

document.onclick = reEnable

7 Comments

Except new Function (return false) is not a valid javascript code. Use just document.onselectstart = disableselect.
@amik how about this: document.oncontextmenu = new Function("return false;"); or this using CSS: <body onselectstart="return false">
Yes new Function("return false;") would work, but is unnecessarily complicated compared to just using disableselect which you already have. onselectstart="return false" would work too, but has nothing to do with CSS. Also I've noticed the rest of your answer is wrong too - the reEnable function wouldn't work at all.
@amik haha well nothing is necessary in JavaScript but we still do it. Certainly, the code can be refactored but take a look at the question, the respond, answers the question not trying to do homework.
Well but SO is a place for education, so I consider providing an answer that is both technically wrong and not explaining at all not a good idea.
|
6

If you got a html page like this:

    <body
    onbeforecopy = "return false"
    ondragstart = "return false" 
    onselectstart = "return false" 
    oncontextmenu = "return false" 
    onselect = "document.selection.empty()" 
    oncopy = "document.selection.empty()">

There a simple way to disable all events:

document.write(document.body.innerHTML)

You got the html content and lost other things.

1 Comment

this is the only solution that worked for me, most of the text on my website pages is populated by js, maybe that's why other solutions didn't work!
5

One might also use, works ok in all browsers, require javascript:

onselectstart = (e) => {e.preventDefault()}

Example:

onselectstart = (e) => {
  e.preventDefault()
  console.log("nope!")
  }
Select me!


One other js alternative, by testing CSS supports, and disable userSelect, or MozUserSelect for Firefox.

let FF
if (CSS.supports("( -moz-user-select: none )")){FF = 1} else {FF = 0}
(FF===1) ? document.body.style.MozUserSelect="none" : document.body.style.userSelect="none"
Select me!


Pure css, same logic. Warning you will have to extend those rules to every browser, this can be verbose.

@supports (user-select:none) {
  div {
    user-select:none
  }
}

@supports (-moz-user-select:none) {
  div {
    -moz-user-select:none
  }
}
<div>Select me!</div>

2 Comments

On Safari Version 14.1.1, the first proposition is supported but not the last ones. On Firefox Developer Edition 92.0b3 (64 bits) all of them are supported.
None of these appear to work on iOS Safari, hmmm... testing with 13.7 i will look at other versions to see if there is some additional progress.
0

Simple Copy this text and put on the before </body>

function disableselect(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function ("return false")

if (window.sidebar) {
  document.onmousedown = disableselect
  document.onclick = reEnable
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.