Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one make a webpage where text is not highlightable?

Example: http://www.snopes.com/business/genius/genius.asp

None of the text is highlightable - I've never seen something like this. Tried to look at the source code, but could not find, not did Google provide any answers.

I'm assuming it's a JS trick? The text is in the source code, so it's not an image. Actually nothing is highlightable on the page.

How is this done?

like image 816
ArtemF Avatar asked May 06 '11 00:05

ArtemF


People also ask

How do I turn off text selection on a web page?

You can use the user-select property to disable text selection of an element. In web browsers, if you double-click on some text it will be selected/highlighted. This property can be used to prevent this.


2 Answers

Read the source code of the page you have linked to:

<script type="text/javascript">
<!--
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
}
-->
</script>

In Chrome, entering document.onselectstart = function() {return false} into the Javascript console seems to do the trick.

But don't do it, it's infuriating.

like image 98
YXD Avatar answered Nov 14 '22 07:11

YXD


This is the script area where they disable the selection:

<script type="text/javascript">
<!--
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
}
-->
</script>

The document.onmousedown = disableselect is the key line.

like image 22
IAmTimCorey Avatar answered Nov 14 '22 09:11

IAmTimCorey