,
Share with your friends 

Disabling Text Selection for HTML Pages - Simple Steps!

0 ratings Views 268 
Author: Raj Kumar_f852f001 (Raj Kumar J)  View Profile |  View other solutions by this author

Question / Problem


How to disable selecting of text in html pages easily using JavaScript?

Solution

With few lines of JavaScript code you can do disable text selection for any html page you code.

Just follow these simple steps:

Step 1:

<!-- Paste this piece of code into an external JavaScript file named: disableTextSelect.js  -->

window.onload = function() {
  document.onselectstart = function() {return false;} // For Internet Explorer
  document.onmousedown = function() {return false;} // For Mozilla Firefox
}

/* You can attach the events to any specific element. In this example, I've disabled text selection for an element with the id 'content'.  */

window.onload = function() {
  var element = document.getElementById('content');
  element.onselectstart = function () { return false; } // For Internet Explorer
  element.onmousedown = function () { return false; } // For Mozilla Firefox
}

Step 2:

<!-- Now just paste this line of code into the HEAD section of your HTML page. Change the path of the file, if needed.  -->

<script type="text/javascript" language="JavaScript" src="disableTextSelect.js"></script>

<!-------------------------------------------->

Another easy way to disable text selection for the entire page.

<!-- Just paste this piece of code into the HEAD section of your HTML page. -->

<script type="text/javascript" language="JavaScript">

function disableText(e) { return false; }
function reEnable() { return true; }

//For browser IE6+
document.onselectstart = new Function ("return false");

//For browser NS6
if (window.sidebar){
document.onmousedown = disableText;
document.onclick = reEnable;
}

</script>

Hope its easy to follow.

Applies to

JavaScript ,HTML

Rank It

Login to rank it

Report


Advertisement