views:

31

answers:

1

I've the following input text box and a button.

<div id="sender" onKeyUp="keypressed(event);">
     Your message: <input type="text" name="msg" size="70" id="msg" />
     <button onClick="doWork();">Send</button>
</div>

The keypressed(event) function actually detects if the key being pressed is "Enter" and calls the doWork() function.

function keypressed(e){
     if(e.keyCode=='13'){
         doWork();
     }
}

This code seems to work fine in Chrome & Firefox. But in IE, the function seems to called twice.

Can anyone help me tweak the code so that it works properly in IE also.

Thanks

EDITED:

Whole ajax codes

<!--
      var httpObject = null;
      var link = "";
      var timerID = 0;
      var nickName = "Unname"; //"<?php echo $nickname; ?>";

      // Get the HTTP Object
      function getHTTPObject(){
         if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
         else if (window.XMLHttpRequest) return new XMLHttpRequest();
         else {
            alert("Your browser does not support AJAX.");
            return null;
         }
      }   

      // Change the value of the outputText field
      function setOutput(){
         if(httpObject.readyState == 4){
            var response = httpObject.responseText;
            var objDiv = document.getElementById("result");
            objDiv.innerHTML += response;
            objDiv.scrollTop = objDiv.scrollHeight;
            var inpObj = document.getElementById("msg");
            inpObj.value = "";
            inpObj.focus();
         }
      }

      // Change the value of the outputText field
      function setAll(){
         if(httpObject.readyState == 4){
            var response = httpObject.responseText;
            var objDiv = document.getElementById("result");
            objDiv.innerHTML = response;
            objDiv.scrollTop = objDiv.scrollHeight;
         }
      }

      // Implement business logic    
      function doWork(){    
         httpObject = getHTTPObject();
         if (httpObject != null) {
            link = "message.php?nick="+nickName+"&msg="+encodeURIComponent(document.getElementById('msg').value);
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setOutput;
            httpObject.send(null);
         }
      }

      // Implement business logic    
      function doReload(){    
         httpObject = getHTTPObject();
         var randomnumber=Math.floor(Math.random()*10000);
         if (httpObject != null) {
            link = "message.php?all=1&rnd="+randomnumber;
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setAll;
            httpObject.send(null);
         }
      }

      function UpdateTimer() {
         doReload();   
         timerID = setTimeout("UpdateTimer()", 5000);
      }


      function keypressed(e){
         if(e.keyCode=='13'){
            doWork();
         }
      }
    //-->
A: 

If someone is facing a similar problem, this could be useful. I solved the problem by detecting IE browser using PHP script and discarding the onKeyUp() function while rendering HTML for IE.

IE Detection PHP script

//Detect IE function
    function detectIE() {
        if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
        return true;
        else
        return false;
    }

And this is how i changed the event handling.

<div id="sender" <?php if (detectIE()) { /*do nothing */ } else { echo "onKeyUp=\"keypressed(event);\""; } ?> >

 Your message: <input type="text" name="msg" size="70" id="msg" />
 <button onClick="doWork();">Send</button>
 </div>
ptamzz