Prevent selecting & copying

前端 未结 3 2071
终归单人心
终归单人心 2021-01-20 13:04

I want to inactive selecting & copying text in html page. when I used Javascript & inactive right click user can use Ctrl+V!!

3条回答
  •  無奈伤痛
    2021-01-20 13:10

    While I agree in principle with the other posters that trying to do this may annoy the user, sometimes a manager or customer demands that this be done, and so an answer needs to be supplied.

    Check out this page on www.dynamicdrive.com that will supply you with a few JavaScripts towards this end. Specifically see "Disable Text Select Script" and "No right click script".

    Disable Text Select Script:

    /***********************************************
    * 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"
    

    No right click script:

    //Disable right mouse click Script
    //By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
    //For full source code, visit http://www.dynamicdrive.com
    var message = "Function Disabled!";
    
    ///////////////////////////////////
    function clickIE4() {
        if (event.button == 2) {
            alert(message);
            return false;
        }
    }
    
    function clickNS4(e) {
        if (document.layers || document.getElementById && !document.all) {
            if (e.which == 2 || e.which == 3) {
                alert(message);
                return false;
            }
        }
    }
    
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown = clickNS4;
    } else if (document.all && !document.getElementById) {
        document.onmousedown = clickIE4;
    }
    
    document.oncontextmenu = new Function("alert(message);return false")
    

提交回复
热议问题