views:

214

answers:

2

How can I prevent IE from refreshing when Ctrl-R is handled in the page by script? This also goes for things like opening the browser history, etc. The solution does not need to work in Non-IE browsers, but it must work in both IE 6 and IE 7.

Is this even possible?

+2  A: 

I dont't think thats possible and I guess it shouldn't be too.

I looks like I must rethink...

Using this library (download link) I could achieve what you want.

Sample code. But the library is able to do much more.

<html>
    <head>
        <title>asd</title>
        <script type="text/javascript" src="shortcut.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            shortcut.add("Ctrl+R",function() {
       alert("Don't reload");
      });
        </script>
    </body>
</html>
jitter
@jitter: Impressive! But sadly this doesn't work for F5 - the shortcut code gets executed, but then the page refreshes anyway. (I'm testing in IE6, mind you - it could be different in other versions.)
RichieHindle
Well C. Ross only requested Ctrl+R
jitter
And you can block F5 with javascript too in IEhttp://paste.bradleygill.com/index.php?paste_id=10382
jitter
I don't care about F5. I don't want to block refresh so much as use the Ctrl+R (Ctrl+I, etc) combination for application specific commands.
C. Ross
+1  A: 

Try this:

<html>
<head>
<script>
function preventStuff () {
   var e = window.event;
   if(e.ctrlKey) {
     switch(e.keyCode) {
     case 82 :
       document.body.innerHTML += "<p>Window refresh prevented.</p>";
       e.returnValue = false;
       break;
     case 72 :
       document.body.innerHTML += "<p>Browser history prevented.</p>";
       e.returnValue = false;
       break;
     }
   }
}
</script>
</head>
<body onkeydown="preventStuff()">
</body>
</html>

"82" is the keyCode for "R", "72" is the keyCode for "H". Add a similar switch case for every key you want to capture.

Tested on IE6 and IE7.

Alsciende
F5 cannot be stopped by javascript, no matter what.
Alsciende
O yes F5 can be stoppedhttp://paste.bradleygill.com/index.php?paste_id=10382
jitter
Oh ok you're writing event.keyCode, I did't see that coming...
Alsciende