• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

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
0
vijayprabakar
Asked:
vijayprabakar
  • 4
  • 3
  • 3
  • +3
1 Solution
 
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
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now