How to block text selection without entering a text field and select items

How to block text selection without entering a text field and select elements In fact, I have a script that works in IE, but not in other browsers, here it is:

 var omitformtags=["input", "textarea", "select"]
 omitformtags=omitformtags.join("|")
 function disableselect(e){
  if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)
   return false
  }
 function reEnable(){
   return true
  }

 if (typeof document.onselectstart!="undefined")
  document.onselectstart=new Function ("return false")
 else{
  document.onmousedown=disableselect
  document.onmouseup=reEnable
 }
  document.onmousedown=click;
  function click()
 {
  if ((event.button==2) || (event.button==3)) { alert("Yazı Kopyalayamazsınız!");}

 }

I need to work the same in other browsers, there must be a ready-made script, I just could not find it on the network.

+3
source share
2 answers
<div onmousedown='return false;' onselectstart='return false;'></div>

onmousedown disables the choice for Firefox, Safari, Opera, Chrome and most other web browsers, onselectstart for IE.

Although disabling text selection and context menus is very bad, and does not prevent pro-users from choosing your text. Simply disabling JavaScript in your browsers allows you to select again.

+4
source

, :

CSS . :

  • no script
  • , script .

Opera IE , html- , . ( IE8, FF3.6, Safari5, opera11, Chrome8):

<html>

<head>

<style>

.unselectable {
    -moz-user-select: none; /* for FireFox */
    -webkit-user-select: none; /* for Chrome and Safari */
    -khtml-user-select: none; /* probably old webkit browsers, but new support it too */
    user-select: none; /* for future CSS3 compliant browsers */
}

</style>

</head>

<body>

<!-- add unselectable class to deny selecting in FF, Chrome, Safari and CSS3 compliant browsers -->
<!-- add unselectable attribute to deny selecting in Opera and IE -->
<div class="unselectable" unselectable="on">test</div>

</body>

</html>

. /mantain. - javascript imho ( html\css , css class\element javascript..)

+2

Source: https://habr.com/ru/post/1784671/


All Articles