Results 1 to 4 of 4

Thread: No select text script

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,127
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default No select text script

    No select text script 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:
    Code:
    var omitformtags=["input", "textarea", "select"]
    Here is the string that results from the join method used:
    Code:
    input|textarea|select
    Here is the test against that string:
    Code:
    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:
    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.
    Last edited by jscheuer1; 07-02-2005 at 06:23 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,127
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    OK, I got IE to append to the onmouseup event (if any) in form tags, rather than co-opt it. Here's the latest:
    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, 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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

    http://www.bandit.co.nz/writings/?tu...Disable_Select

  4. #4
    Join Date
    Oct 2006
    Location
    U.S
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •