开发者

JavaScript not working in Firefox

开发者 https://www.devze.com 2022-12-20 21:17 出处:网络
Hi \' im having a problem with my scri开发者_运维问答pt in firefox, i\'m getting the fault , \" window.event is undefined \" , where seems to be the problem?

Hi ' im having a problem with my scri开发者_运维问答pt in firefox, i'm getting the fault , " window.event is undefined " , where seems to be the problem?

<script language="JavaScript1.2"> 

function formattel(){

 var value = document.formkkush.telefoon.value; 
 var valueLen = document.formkkush.telefoon.value.length; 

 if(valueLen>2 && valueLen<4) 
 { 
 if(window.event.keyCode==8) 
 document.formkkush.telefoon.value=document.formkkush.telefoon.value.remove(valueLen-1,1); 
 else 
 document.formkkush.telefoon.value=document.formkkush.telefoon.value+"/"; 
 }
 if(valueLen>5 && valueLen<7) 
 { 
 if(window.event.keyCode==8) 
 document.formkkush.telefoon.value=document.formkkush.telefoon.value.remove(valueLen-1,1); 
 else 
 document.formkkush.telefoon.value=document.formkkush.telefoon.value+"."; 
 }
 if(valueLen>8 && valueLen<10) 
 { 
 if(window.event.keyCode==8) 
 document.formkkush.telefoon.value=document.formkkush.telefoon.value.remove(valueLen-1,1); 
 else 
 document.formkkush.telefoon.value=document.formkkush.telefoon.value+"."; 
 }
} 


The global event object isn't created in Firefox. It's instead passed as the first argument in the event handler's function.

function formattel(eventObj){ 
  var value = document.formkkush.telefoon.value;  
  var valueLen = document.formkkush.telefoon.value.length;  

  // Make sure we have the even object no matter which browser we're running on
  eventObj = eventObj || window.event;

  if(valueLen>2 && valueLen<4)  
  {  
    if(eventObj.keyCode==8)  
      document.formkkush.telefoon.value=document.formkkush.telefoon.value.remove(valueLen-1,1);  
    else  
      document.formkkush.telefoon.value=document.formkkush.telefoon.value+"/";  
  } 
  // etc...
}  

See the Mozilla documentation for event.

As a completely unrelated suggestion, you should try and lessen the number of DOM node lookups by setting repeated lookups to a variable. It makes your code faster and much more presentable:

function formattel(eventObj){ 

  var telefoon = document.formkkush.telefoon; 
  var value = telefoon.value;  
  var valueLen = value.length;  

  // Make sure we have the even object no matter which browser we're running on
  eventObj = eventObj || window.event;

  if(valueLen>2 && valueLen<4)  
  {  
    if(eventObj.keyCode==8)  
      telefoon.value = telefoon.value.remove(valueLen-1,1);  
    else  
      telefoon.value += "/";  
  } 
  // etc...
}  

Don't forget about assignment operators (such as +=) too!

0

精彩评论

暂无评论...
验证码 换一张
取 消