PDA

View Full Version : No select text script



jscheuer1
07-02-2005, 05:38 AM
No select text script (http://www.dynamicdrive.com/dynamicindex9/noselect.htm) has a bug in Mozilla. Because the array of tag names to be excluded in NS6+ (Mozilla) is joined into a string before the target tags are tested against it, unintended tags get excluded from the no select text treatment. Here is the array from the demo:
var omitformtags=["input", "textarea", "select"]Here is the string that results from the join method used:
input|textarea|selectHere is the test against that string:
if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)Basically, if the tag's lower case name is in that string, it is exempt from the no select treatment. This will include <input>, <textarea> and <select> to be sure but, also <p> and <a> tags. I've come up with a simple remedy which uses the array as is, without joining it to a string. I've also got a method to exclude these same areas under IE6 but, it co-opts the onmouseup event for those elements. Not such a big sacrifice as other events can be used in most cases to work around that. Here is the code:
<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 form tags properly and cross browser by jscheuer1
***********************************************/

//form tags to omit:
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
}

function noSelect(){
if (typeof document.onselectstart!="undefined"){
document.onselectstart=new Function ("return false")
if (document.getElementsByTagName){
tags=document.getElementsByTagName('*')
for (j = 0; j < tags.length; j++){
for (i = 0; i < omitformtags.length; i++)
if (tags[j].tagName.toLowerCase()==omitformtags[i]){
tags[j].onselectstart=function(){
document.onselectstart=new Function ('return true')
}
tags[j].onmouseup=function(){
document.onselectstart=new Function ('return false')
}
}
}
}
}
else{
document.onmousedown=disableselect
document.onmouseup=reEnable
}
}

window.onload=noSelect;
</script>Hopefully this can be simplified and/or made to append to the onmouseup event in IE for protected tags, rather than co-opting it.

jscheuer1
07-02-2005, 09:06 AM
OK, I got IE to append to the onmouseup event (if any) in form tags, rather than co-opt it. Here's the latest:
<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 form tags properly, cross browser by jscheuer1
***********************************************/

//form tags to omit:
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
}

function noSelect(){
if (typeof document.onselectstart!="undefined"){
document.onselectstart=new Function ("return false")
if (document.getElementsByTagName){
tags=document.getElementsByTagName('*')
for (j = 0; j < tags.length; j++){
for (i = 0; i < omitformtags.length; i++)
if (tags[j].tagName.toLowerCase()==omitformtags[i]){
tags[j].onselectstart=function(){
document.onselectstart=new Function ('return true')
}
if (tags[j].onmouseup!==null){
var mUp=tags[j].onmouseup.toString()
mUp='document.onselectstart=new Function (\'return false\');\n'+mUp.substr(mUp.indexOf('{')+2,mUp.lastIndexOf('}')-mUp.indexOf('{')-3);
tags[j].onmouseup=new Function(mUp);
}
else{
tags[j].onmouseup=function(){
document.onselectstart=new Function ('return false')
}
}
}
}
}
}
else{
document.onmousedown=disableselect
document.onmouseup=reEnable
}
}

window.onload=noSelect;
</script>

bandit
11-12-2005, 01:42 PM
Hello

After finding this thread via a Google search - and then finding the script doesn't work properly, I wrote my own.

Anyway, I thought others that also find this thread might find my version useful, so here's a linky:

www.bandit.co.nz/writings/?tut=Javascript_Disable_Select

x218o7x
10-08-2006, 05:53 AM
i didnt try your script bandit but jscheuer1 dude your a true genius, that script is working perfect for me in Firefox 1.5.0.7... i didnt understand a word you said but if it works with IE as well then I think it should replace http://dynamicdrive.com/dynamicindex9/noselect.htm