Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2004, 01:24 PM
Authorized User
 
Join Date: Nov 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to hijack backspace onkeypress (or keydown)?

Hi;

I'm trying to write a simple HTMLArea like rich text editor. I'm doing that by changing innerHTML attribute of any tag that supports that on onkeypress event of document. It is easy to write stuff on page that way with unicode caracters, however when the time came to delete stuff by clicking backspace I had a problem. Browser's default behavior is to go to previous page on backspace press. (onkeypress event didn't work either, it doesn't get backspace's ASCII code, so I used onkeydown just for backspace press). I returned false on onKeyDown event if the ASCII code is 70 to prevent browser's default "go to previous page" behavior, but it didn't work. So I also returned false if key was 70 on onKeyUp, still wasn't enough. I can't cancel onkeypress event's backspace default behavior, because it doesn't have any ASCII entry when backspace is clicked. Here is the part of the code I'm using:

function onKeyDown(e){
e=window.event;
var keyco =new Number();
keyco =e.keyCode;
alert(keyco);
    if (keyco==70)
    {
        return false;
    }
}

document.onkeydown = onKeyDown

Any help would be appreciated, thanks.
 
Old November 8th, 2004, 05:41 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This seems to work:
Code:
<html>
<head>
<title>Cancel Backspace</title>
<script type="text/javascript">  

  function handleKeyDown()
  {
    var src = window.event.srcElement;
    if (src.getAttribute("editable") == "true")
    {
      var iCode = window.event.keyCode;
      if (iCode == 8)
      {        
        src.value += "{BACKSPACE}";
        window.event.returnValue = false;
      }
    }
  }

  function init()
  {
    document.onkeydown = handleKeyDown;
  }  
</script>
</head>
<body onload="init();">
<input type="text" id="txtInput" editable="true" style="position:relative; width=500px;border: 3px inset #c0c0c0;"></div>
</body>
</html>
--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
Hijack or dishonest coding in packaged CSS papamike69 Need help with your homework? 4 March 27th, 2008 08:53 AM
Page expires on hitting backspace! nishant_611 Pro JSP 0 May 9th, 2007 01:26 AM
Page Expires on hitting backspace. nishant_611 JSP Basics 0 May 9th, 2007 01:22 AM
Page Expires on hitting backspace. nishant_611 Apache Tomcat 0 May 9th, 2007 01:19 AM
Onkeypress Fia VB.NET 2002/2003 Basics 0 May 6th, 2005 05:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.