0

I am using below script to stop copy, paste, f12, ctr+u etc and disable mouse right click.

    <script> var message=&quot;Copyright 2017-2018&quot;;

document.onkeydown = function(e) {
  if(e.ctrlKey &amp;&amp; e.shiftKey &amp;&amp; e.which==73)
    return false;

  if (e.which == 123)
    return false;

  if (e.ctrlKey &amp;&amp; ((e.which == 85) || (e.which == 65) || (e.which == 88) || (e.which == 67) || (e.which == 2) || (e.which == 3) || (e.which == 123) || (e.which == 83))) {
    alert(message);
    return false;
  }
}

// right click code
var isNS = (navigator.appName == &quot;Netscape&quot;) ? 1 : 0;
if (navigator.appName == &quot;Netscape&quot;)
  document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP);

function mischandler() {
  return false;
}

function mousehandler(e) {
  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
  if ((eventbutton == 2) || (eventbutton == 3)) return false;
}

document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;

//select content code disable  alok goyal
function killCopy(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function(&quot;return false&quot;)
if (window.sidebar) {
  document.onmousedown = killCopy
  document.onclick = reEnable
}
    </script>

I want to enable only copy and paste feature on my search bar. Below is my search bar code.

<div class="search-input">
<input aria-label="Search this blog" autocomplete="off" name="q" placeholder="हिंदी में ही लिखें" value="">
</div>

Is there is any way to only enable copy and paste feature on the search bar.

Akash Chaubey
  • 43
  • 1
  • 5
  • 1
    Any code that attempts to prevent right-clicking and keyboard commands can easily be overcome. This is a futile effort and a bad idea from a UI design perspective. – Scott Marcus May 24 '18 at 18:41
  • 1
    You know that F12 has also a menu item...you can check focused control (by class or input type) but PLEASE PLEASE PLEASE don't do it. I HATE when copy & paste is disabled. – Adriano Repetti May 24 '18 at 18:45

1 Answers1

0

As mentioned in the comments, disabling copy-paste is a bad idea, as it will make users confused. Does my clipboard still work? Or is this site broken? Besides, it could be activated through the Developer Tools anyway.

However, if you still want to continue:

window.onload = function() {
  Array.prototype.slice.call(document.querySelectorAll("input:not(#search)"))
 .forEach(function(object){object.onpaste = function(e) {e.preventDefault();}})
}
<input id="search" placeholder="Search">

<input>
<input>
<input>

Based upon Disable copy paste in HTML input fields?

Ward Segers
  • 519
  • 5
  • 17
  • 1
    `.querySelectorAll().forEach()` is not supported in all browsers and so the resulting node list should be converted to an `Array` for compatibility with `Array.prototype.slice.call(document.querySelectorAll()).forEach()`. – Scott Marcus May 24 '18 at 21:22
  • It's work only in desktop. But in mobile, I am not able to paste in the search bar. – Akash Chaubey May 25 '18 at 03:03
  • Enhanced solution after the comment of @ScottMarcus – Ward Segers May 25 '18 at 07:17