View Full Version : ondblclick highlights page text
sherpi
06-30-2005, 12:41 AM
Hi,
is there anyway to un-highlight the highlighted text caused by a dblclick....we are using the ondblclick JS event for a web app. we are also trying to not make this too complicated or interfere with all the other JS used......
thanks
glenngv
06-30-2005, 05:41 AM
IE only.
<div onselectstart="return false">You cannot highlight me</div>
Take note that this poses an accessibility issue because the user won't be able to copy the text.
jscheuer1
07-01-2005, 11:11 AM
Dynamic Drive has a no select text script:
http://www.dynamicdrive.com/dynamicindex9/noselect.htm
It is cross browser but, a little buggy as, in Mozilla the exceptions are broader than they should be. Removing this line:if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)Will tighten it up but, possibly make it too restrictive. Items can be excepted using id's if needed.
Still suffers from:
this poses an accessibility issue
jscheuer1
07-01-2005, 12:59 PM
I got into this one a bit more, I know you said you didn't want it too complicated but, this isn't that complicated. It shows how to properly exclude tags in Mozilla and an avenue (on a case by case basis) for excluding tags in IE. I threw in a link with onmousedown and a document wide ondblclick function for testing purposes:
<html>
<head>
<title>No Select w/dblclick event - Demo</title>
<script type="text/javascript">
/***********************************************
* Disable select-text script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
* Modified here to exclude tags properly in Mozilla by jscheuer1
***********************************************/
//form tags to omit in Mozilla:
var omitformtags=["input", "textarea", "select"]
function disableselect(e){
for (i = 0; i < omitformtags.length; i++)
if (omitformtags[i]==(e.target.tagName.toLowerCase()))
return;
return false
}
function reEnable(){
return true
}
if (typeof document.onselectstart!="undefined")
document.onselectstart=new Function ("return false")
else{
document.onmousedown=disableselect
document.onmouseup=reEnable
}
//end disable select code
document.ondblclick=new Function ("alert('ha')")
</script>
</head>
<body>
<p>IJHBKJVOH Highlight Me! BGUOBHVZH;UEHU<BR>GJHBV BGHBG;uBHV G;UR</p>
<input type="text" value="No! Highlight Me!" onselectstart="document.onselectstart=new Function ('return true')" onmouseup="document.onselectstart=new Function ('return false')">
<br><a href="#" onmousedown="alert('working');return false">Click Me!</a>
</body>
</html>I've come up with an even better version, posted at:
http://www.dynamicdrive.com/forums/showthread.php?p=11939#post11939
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.