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开发者_Go百科 this done?
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.
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.
There are also ways that don't require JS,
-moz-user-select (or -khtml-user-select for safari/chrome)
For IE/opera the unselectable-attribute
It's this part of the page source:
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;
}
精彩评论