1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do you disable highlighting of text on an html page?

Discussion in 'HTML & Website Design' started by Reminisce, May 19, 2008.

  1. #1
    I already put a code to not allow right clicking, now I need to prevent highlighting of text; but I know nothing of html.

    Thanks in advance
     
    Reminisce, May 19, 2008 IP
  2. JJ-sama

    JJ-sama Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Frankly, you could've easily found what you're looking for on Google :)

    Here's something from Dynamic Drive:

    Add this script to the HEAD section of your page:
    <script type="text/javascript">
    
    /***********************************************
    * Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    function disableSelection(target){
    if (typeof target.onselectstart!="undefined") //IE route
    	target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
    	target.style.MozUserSelect="none"
    else //All other route (ie: Opera)
    	target.onmousedown=function(){return false}
    target.style.cursor = "default"
    }
    
    //Sample usages
    //disableSelection(document.body) //Disable text selection on entire body
    //disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"
    
    </script>
    Code (markup):
    With the script installed, just call the function disableSelection(target) at the very end of the document with a reference to the element you wish to disable text within. A few examples:

    Disable text selection on ENTIRE BODY of page:
    <script type="text/javascript">
    disableSelection(document.body) //disable text selection on entire body of page
    </script>
    Code (markup):
    Disable text selection with DIV with id="mydiv" << you'll probably have different names for your DIVs:
    <script type="text/javascript">
    var somediv=document.getElementById("mydiv")
    disableSelection(somediv) //disable text selection within DIV with id="mydiv"
    </script>
    Code (markup):
    Disable text selection within all tables on the page:
    <script type="text/javascript">
    var alltables=document.getElementsByTagName("table")
    for (var i=0; i<alltables.length; i++)
    disableSelection(alltables[i]) //disable text selection within all tables on the page
    </script>
    Code (markup):
     
    JJ-sama, May 19, 2008 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Which won't work in Opera, and most likely not Firefox or Safari.

    Best scrap the idea altogether. Nothing says "Please leave this page" when I can't highlight what I read.
     
    blueparukia, May 19, 2008 IP
  4. JJ-sama

    JJ-sama Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I kind of agree with this.

    That script from DynamicDrive does work on FireFox, but I don't know about all the other browsers. It's easy to disable it though; simply disable Javascript on your browser.

    If you're (thread starter) afraid of people stealing from your site, the best and only solution is not to publish it online (at all). Disabling right click and highlighting won't stop people from stealing from your site.

    G'luck
     
    JJ-sama, May 20, 2008 IP
  5. dylanj

    dylanj Peon

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Actually, I have tested that script and I know for sure that it works in Firefox. Because of the fact that both Firefox and Safari follow very similar web standards, that probably means that it would work in Safari also.
     
    dylanj, May 20, 2008 IP
  6. konrad

    konrad Peon

    Messages:
    808
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why do you want to prevent highlighting?
     
    konrad, May 20, 2008 IP
  7. xJSGx

    xJSGx Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    True!!!! Very... however if it is protected content or certain content... I'd go with that....

    But highlighting isnt this guys problem... REMEMBER::: text or any html for that matter is read by the client computer... So hiding it is near impossible 100% if it is viewable...

    Tips::: Disable the ctrl key, disable the menu - edit ->copy/paste options,
    and anything you want to protect, create dynamically or dynamicly
    load it... ie... using php you can hide .js file content well...

    probly not all you can do but just some pointers.

    There are a lot of reasons to disable highlighting.... For me it is temporary. When someone on mouses down to move an object ; for instace a image; (saying your dragging funcs are made) if the mouse moves too quickly it rolls off of the image and highlights the page for a second. even itself at times...

    so the obvious course of action is to dissable it on mouse out while mouse down then enable it on mouse up...
     
    xJSGx, Feb 21, 2009 IP
  8. innovati

    innovati Peon

    Messages:
    948
    Likes Received:
    63
    Best Answers:
    1
    Trophy Points:
    0
    #8
    IF YOU DONT WANT IT TO BE STOLEN, DONT PUT IT ONLINE.

    The nature of the internet states that before they can view it on their machine, it's sent a copy of the text/files to their computer already. Theres already a file on their computer that contains everything they see even before it shows up to their eyes. Preventing highlighting is a dick move, and it will drive people away from your site faster than anything.

    Personally, I highlight sections as I go along like one might trace their finger along a line as they read it. It's how I keep track of where I am. Prevent me from doing that an I won't read it, I'll simply leave your page and never go back.
    As for right clicking, as I browse online I often have 4 or 5 tabs open, I open links in new tabs that I am interested in reading by rightclicking. If you disable that it's a slap in the face.

    If I wanted your image that you want to keep, I don't need to right click to get it if I want it, I can take a screenshot, I can save the entire page, and fish the image out of the folder, I can hit control-I in firefox and save the images on the page individually or I can go into my browser cache and fish the image out of where it's already on my hard drive.

    If I really want it, I have PLENTY of way to get it, but if you disable my right click, that's a slap in the face, spitting in my eye and then telling me you don't want me to see your website. So I won't. End of story.

    It sounds to me like you have control issues over your content (or maybe it's not even your content). This is the internet, the nature of it is open, and if you don't like that, go print a brochure and stay off the internet until you're ready to be mature and do things the way they've been done for 20 years.

    Good luck to you, and I really hope you smarten up before you alienate every single person you are aiming your site towards.
     
    innovati, Feb 21, 2009 IP
    Grit. likes this.
  9. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Not to mention people like me will go out there way to obtain your "protected" content just because you've tried to protect it :)
     
    wd_2k6, Feb 22, 2009 IP
  10. walshms

    walshms Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Did it ever occur to anyone that you should disable highlighting if you're writing a touchscreen/kiosk application? FYI that's what I'm doing. I came here to find the answer to this and found another site instead. I came back to blow your minds in case you never thought of a valid reason to disable highlighting. Also, lighten the F up. No one is spitting in your eye.
     
    walshms, Sep 9, 2010 IP
  11. jpveneracion

    jpveneracion Well-Known Member

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    106
    #11
    or for instance, i am doing a div which visitors need to double click to show another div... (kind of like a switch) but it's ugly to get the text highlighted when the divs gets double clicked...
     
    jpveneracion, Nov 11, 2010 IP
  12. moshecogan

    moshecogan Active Member

    Messages:
    265
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #12
    Why do you want to prevent highlighting?
     
    moshecogan, Nov 24, 2010 IP
  13. khanh4all

    khanh4all Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    Hi JJ-sama, your solution works 100% for me. Thanks so much
     
    khanh4all, Mar 14, 2013 IP