l'événement onKeyPress ne fonctionne pas sous Firefox

J'ai le code javascript suivant...Ici, je suis en utilisant onKeyPress="someFunction( )" dans la balise body pour obtenir le keyCode de la touche sur laquelle vous appuyez.

Le code fonctionne très bien dans IE8 mais c'est ne fonctionne pas sous Firefox.

Merci de donner la solution à ce.

<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()">

Merci d'avance.....


Voici le code qui fonctionne très bien dans IE8 et ne fonctionne pas sous Firefox.

<!DOCTYPE html>
<html>
<head>
<title>Please help 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)">
&nbsp;&nbsp;<b><u>P</u></b>rint&nbsp;&nbsp;
</button>
<button value="exit" accesskey="E" onclick="keypress(this.value)">
&nbsp;&nbsp;&nbsp;<b><u>E</u></b>xit&nbsp;&nbsp;&nbsp;
</button>
</td></tr>
</table> 
<h3>Press the letters P->Print , E->Exit etc....</h3>   
<h1>Just a test for keypress event</h1>
<form action="http://google.co.in" method="Post">
<div id="prnt">
zzzzzzzzzzzzzzz
</div>
</form>
</td>
</tr>
</tbody>
</table></body></html>
  • Bienvenue sur le site
  • Pourriez-vous décrire exactement ce que vous essayez d'atteindre?
  • Je suis en train d'affecter une fonction(dire l'Impression de la page) à la touche " P " par le biais de l'événement onKeyPress. Je suis avec Windows XP , IE8 , Firefox. Mon problème est le suivant: Dans Firefox, l'événement onKeyPress pas le retour de tout mot de code que j'ai besoin d'appeler une fonction spécifique. Mais dans IE8 son travail.
InformationsquelleAutor Nirmal | 2010-12-21