i have following javascript code...here using onkeypress="somefunction( )" in body tag keycode of key pressed.
the code working fine in ie8 not working in firefox.
please give solution this.
<html> <head> <title>onkeypress( ) event not working in firefox..</title> <script> function printdiv() { var divtoprint=document.getelementbyid('prnt'); newwin=window.open(''+self.location,'printwin','left=50,top=20,width=590,height=840,toolbar=1,resizable=1,scrollbars=yes'); newwin.document.write(divtoprint.outerhtml); newwin.print(); //newwin.close(); } </script> <script> function keypress() { alert(event.keycode); var key=event.keycode; if(key==112 || key==80) printdiv(); else if(key==101 || key==69) window.location="http://google.com"; else if(key==114 || key==82) window.reset(); } </script> </head> <body bgcolor="lightblue" onkeypress="keypress()">
thanks in advance.....
here total code working fine in ie8 , not working in firefox.
<!doctype html> <html> <head> <title>please me out</title> <script type="text/javascript"> function printdiv() { var divtoprint=document.getelementbyid('prnt'); newwin=window.open(''+self.location,'printwin','left=50,top=20,width=590,height=840,toolbar=1,resizable=1,scrollbars=yes'); newwin.document.write(divtoprint.outerhtml); newwin.print(); } </script> <script type="text/javascript"> function keypress(val) { //----------------------------------------------------- //alert('nnnn'); //alert(window.event ? event.keycode : val.which); //if(val.which != 0 && val.charcode != 0) // alert('firefox'+string.fromcharcode(val.which)); //else // alert('ie'); //------------------------------------------------------- var key=event.keycode; if(key==112 || key==80 || val=="print") printdiv(); else if(key==101 || key==69 || val=="exit") window.location="http://google.co.in"; else if(key==114 || key==82 || val=="refresh") document.forms[0].reset(); else event.returnvalue=true; } </script> </head> <body bgcolor="lightblue" topmargin="0" leftmargin="0"marginwidth="0px" marginheight="0px" onkeypress="keypress(null)"> <table align="left" border="1" cellpadding="5" cellspacing="0" style="width: 100%;height:100%"> <tbody> <tr><td width="20%" valign="top">ccccccccccc</td> <td width="80%" align="center"> <table style="width: 100%" border="0" valign="top"> <tr align="right"> <td valign="top"> <button value="refresh" accesskey="r" onclick="keypress(this.value)"> <b><u>r</u></b>efresh </button> <button value="print" accesskey="p" onclick="keypress(this.value)"> <b><u>p</u></b>rint </button> <button value="exit" accesskey="e" onclick="keypress(this.value)"> <b><u>e</u></b>xit </button> </td></tr> </table> <h3>press letters p->print , e->exit etc....</h3> <h1>just test keypress event</h1> <form action="http://google.co.in" method="post"> <div id="prnt"> zzzzzzzzzzzzzzz </div> </form> </td> </tr> </tbody> </table></body></html>
when problems show up, start use kind of javascript framework. frameworks build avoid problems different browsers.
to catch different keypress()
apis, link emmett shows, can difficult.
example:
in html head:
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
in js tag:
$(document).keydown(function(event) { alert('you pressed '+event.keycode); event.preventdefault(); });
Comments
Post a Comment