Disabling Refresh and Right Click

Hi Experts,

I need code to avoid right click and refresh/reload in the web page. Please advice.

Cheers,
Vijay
vijayprabakarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
0
Roman GhermanSenior Software EngineerCommented:
You can definitely not prevent Refresh/Reload
what about right click, the above solution is good - simple javascript. Here is a cool jQuery solution:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>jQuery disable right click easily</title>
    <script language="javascript" type="text/javascript"
      src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
    </script>
    <script type="text/javascript" language="javascript">
        $(function() {
            $(this).bind("contextmenu", function(e) {
                e.preventDefault();
            });
        }); 
    </script>
</head>
<body>
    Your text here
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
remixedcatCommented:
Here's my version without the message pop-up.

<script language=JavaScript>
<!--

//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){
;
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
;
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(";return false")

// --> 
</script>

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Roman GhermanSenior Software EngineerCommented:
@remixedcat - that is not your versions, that is the same but without alerts + with dirty code.
0
vijayprabakarAuthor Commented:
In HDFC bank site, the refresh is disabled.
0
Roman GhermanSenior Software EngineerCommented:
that is not possible, can you show a screenshot.
What browser are you using?
What is the link of that bank?
0
remixedcatCommented:
roma--->It's my version of the other DD code without the pop-up. I modded it to not have the pop-up.
0
Michel PlungjanIT ExpertCommented:
You cannot disable any of this. I can easily bypass anything you do to stop it.

Please instead tell us what the problem is.

99% of all questions I have had on this came down to the server process not being able to HANDLE the refresh and right-click, open in new window.

It needs to handle that, then you do not need to block it.
0
vivekpv10Commented:
@roma1123...In this site,its handling that refresh button.
https://netbanking.hdfcbank.com/netbanking/
0
vivekpv10Commented:
@vijayprabakar, I thing this is exactly that u needed..
http://javascript.internet.com/page-details/no-right-click.html
0
Roman GhermanSenior Software EngineerCommented:
hehe,

@vivekpv10.... Actually the refresh button is not disabled, what they did is actually disable user from navigating using keyboard, including prevent user from pressing F5.
But Browser's refresh button is still available, i can also go and repaste the link and it will refresh the page.

I also agree with @mplungjan - "You cannot disable any of this. I can easily bypass anything you do to stop it."

It is very easy to bypass.

By the way here is what they are doing on theirs site to "prevent" refreshing and right click:
//------------------------------------------------------------------------------------------------

function callInit(){



    try {

        var useragent = new String(navigator.userAgent); /* BrowserComp:ADD-25/11/2010-MohsinK */



        document.onkeydown      = disablekeyboardnavigation;



        if(isIE5){

            document.onmousedown = disablerightclick;



        /* BrowserComp:ADD-25/11/2010-MohsinK-START */



        }else if((useragent.indexOf("Chrome") != -1) || (useragent.indexOf("Safari") != -1)){

            isChSaf = true;

            document.onmouseup   = disablerightclick;



        /* BrowserComp:ADD-25/11/2010-MohsinK-END */    



        } else {

            document.onclick     = disablerightclick;

        }



        if (document.layers || (!document.all && document.getElementById)) {

            //alert('layers');

            window.captureEvents(Event.MOUSEDOWN);

            document.captureEvents(Event.KEYDOWN);

        }

    } catch (e0) {

        //alert('RS_01_eng[callInit] error occurred => '+e0.message);

    }

}

//-------------------------------------------------------------------------------------------------

function disablerightclick(e){

    var mouse_click = "";



    try {

        /* BrowserComp:ADD-25/11/2010-MohsinK-START */

        

        if(isChSaf){

            mouse_click = e.which;



        /* BrowserComp:ADD-25/11/2010-MohsinK-END */

        }else if(window.event) {/* BrowserComp:MOD-25/11/2010-MohsinK */

            mouse_click = event.button;     //IE

        } else {

            mouse_click = e.which;          //firefox

        }



        /* BrowserComp:ADD-25/11/2010-MohsinK-START */

        if(isChSaf && mouse_click == 3 && (event.srcElement.tagName != "U")){

            document.oncontextmenu = returnFalse;

        }



        if(isChSaf && (event.srcElement.tagName == "U")){

            document.oncontextmenu = returnTrue;

        }

        /* BrowserComp:ADD-25/11/2010-MohsinK-END */



        //alert('navigator---'+navigator.appName+', mouse_click--'+mouse_click+', isIE5-'+isIE5);

        if (mouse_click == 2 || mouse_click == 3 || mouse_click == 4){

            if (isIE5 && mouse_click == 2 && (event.srcElement.tagName != "U")){

                alert("Mouse Right Click Disabled");

                return false;

            } else if(!isIE5 && mouse_click == 3 && (e.target.tagName != 'U')){

                alert("Mouse Right Click Disabled");

                e.preventDefault();

                e.stopPropagation();

                return false;

            }

        }

    } catch (e1) {

        //alert('RS_01_eng[disablerightclick] error occurred => '+e.message);

        mouse_click = "";

    }   

    return true;

}

//-------------------------------------------------------------------------------------------------

function disablekeyboardnavigation(e)

{

    var input_key   = "";



    try {

        if(window.event) {

            input_key   = event.keyCode;    //IE

        } else {

            input_key   = e.which;          //firefox

        }

        //alert('navigator---'+navigator.appName+', input_key--'+input_key);



        if (input_key == 8) //Back

        { 

            if (isIE5 && event.srcElement.tagName != "INPUT" && event.srcElement.tagName != "TEXTAREA"){

                //alert('keyboard-Back...not allowed..'+event.srcElement.tagName);  

                alert('This operation is not allowed');

                return false; 

            }

            if (!isIE5 && e.target.tagName != 'INPUT' && e.target.tagName != 'TEXTAREA'){

                //alert('keyboard-Back...not allowed..'+e.target.tagName);  

                alert('This operation is not allowed');

                return false; 

            }

        }

        if((input_key == 36 || input_key == 37 || input_key == 39) && input_key == 18) //Alt + Home, Ltarrw, RtArrw

        {

            //alert('RS_01_eng[disablerightclick]...Alt + LtAr/RtAr/Hm-'+input_key);    //not working

            alert('This operation is not allowed');

            return false;

        }

        if (input_key == 27) //Esc button

        {

            //alert("keyboard-Esc...not allowed - "+input_key);  

            alert('This operation is not allowed');

            return false;

        }

        if (event.keyCode == 116 || event.keyCode == 121 || event.keyCode == 122)   //F5, F10, F11

        {

            if (isIE5){

                event.keyCode = 0;

            } else {

                e.preventDefault();

                e.stopPropagation();

            }

            //alert('RS_01_eng[disablerightclick]...F5/F10/F11-'+input_key);

            alert('This operation is not allowed');

            return false;

        }

        

    } catch (e2) {

        //alert('RS_01_eng[disablerightclick] error occurred => '+e.message);

        input_key = "";

    }   

    return true;

}

Open in new window

0
remixedcatCommented:
well the code I've provided works in FF4.
0
vijayprabakarAuthor Commented:
Should we have to use Synchronizer pattern to stop refresh of the page?
0
Michel PlungjanIT ExpertCommented:
What does that mean?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.