Luis Campos
Luis Campos

Reputation: 81

javascript and ajax problems in IE and Safari

i made a code in javascript to help me manage my page, but it seems to be having some problems working in IE and Safari, it works perfectly in firefox,like usual, but in the other browers is not working. the function toggle is to select all visisle checkboxes, the send sms will capture all values of the checkboxes and go to another link, and filter table, will help to filter the content that is on the table.

    <script type="text/javascript">
function toggle() {
  var checkboxes = document.getElementsByName('parasms');
  for each(var checkbox in checkboxes)
    checkbox.checked = true;
}
function toggle2() {
   var checkboxes = document.getElementsByName('parasms');
  for each(var checkbox in checkboxes)
    checkbox.checked = false;
}

function sendsms()
{
    var counter = 0,
        i = 0,  
        url = '', 
        input_obj = document.getElementsByTagName('input');
    for (i = 0; i < input_obj.length; i++) {
        if (input_obj[i].type === 'checkbox' && input_obj[i].checked === true) {
            counter++;
            url = url + ', ' + input_obj[i].value;
        }
    }
    if (counter > 0) {
        url = url.substr(1);
        window.location.href = 'enviarsms.php?num=' + url; 
    }
}
function clean()
{
         document.getElementById('empresa').value="";
         document.getElementById('numero').value="";
         document.getElementById('utilizador').value="";
         document.getElementById('pontos').value="";
         document.getElementById('movel').value="";
         document.getElementById('actividade').value="";
         document.getElementById('email').value="";   
}
function filtertable(idElm)
{         
var empresa=document.getElementById('empresa').value;
var numero=document.getElementById('numero').value;
var utilizador=document.getElementById('utilizador').value;
var localidade=document.getElementById('localidade').value;
var pontos=document.getElementById('pontos').value;
var movel=document.getElementById('movel').value;
var actividade=document.getElementById('actividade').value;
var email=document.getElementById('email').value;

if (empresa=="" && numero==""&& utilizador=="" && localidade=="" && movel=="" && actividade=="" && email=="" && pontos=="")
  {
   window.location = "../index.php";
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
   xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    { 
         document.getElementById('tbl1').innerHTML=xmlhttp.responseText;
         document.getElementById(idElm).focus();
         document.getElementById('empresa').value=empresa;
         document.getElementById('numero').value=numero;
         document.getElementById('utilizador').value=utilizador;
         document.getElementById('localidade').value=localidade;
         document.getElementById('pontos').value=pontos;
         document.getElementById('movel').value=movel;
         document.getElementById('actividade').value=actividade;
         document.getElementById('email').value=email;        
         return;
    } 
  }
xmlhttp.open("GET","filtertable.php?empresa="+empresa+"&&numero="+numero+"&&utilizador="+utilizador+"&&localidade="+localidade+"&&pontos="+pontos+"&&movel="+movel+"&&actividade="+actividade+"&&email="+email+"&&random=" + Math.random(), true);
xmlhttp.send();
}
</script>
    <table width="2000" border="0" cellspacing="2" cellpadding="2" align="center" id="tbl1" name="representantes" class="table1">  
    <tr><td colspan="9"><a href="inserirrepresentante.php">Inserir Representante</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="sendsms()">Enviar SMS</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="toggle();">Seleccionar tudo</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="toggle2();">Desselecionar</a></td></tr>
    <tr style="background-color: grey;color:white;text-align: center;"><th style="text-align:center;">Acções</th><th>Empresa</th><th style="text-align:center;">Numero</th><th style="text-align:center;">Utilizador</th><th style="text-align:center;">Localidade</th><th style="text-align:center;">Pontos</th><th style="text-align:center;">Telefone Movel</th><th style="text-align:center;">Actividade</th><th style="text-align:center;width: 160px; max-width: 160px;overflow-x: hidden;">Email</th></tr>
    <tr>
    <th></th>    
    <th><input size="45" type="text" id="empresa" onkeyup="filtertable('empresa')" style="border:1px dashed black;"/></th>
    <th><input size="10" type="text" id="numero" onkeyup="filtertable('numero')" style="border:1px dashed black;" /></th>
    <th><input size="25" type="text" id="utilizador" onkeyup="filtertable('utilizador')" style="border:1px dashed black;" /></th>
    <th><input size="15" type="text" id="localidade" onkeyup="filtertable('localidade')" style="border:1px dashed black;" /></th>
    <th><input size="10" type="text" id="pontos" onkeyup="filtertable('pontos')" style="border:1px dashed black;" /></th>
    <th><input size="20" type="text" id="movel" onkeyup="filtertable('movel')" style="border:1px dashed black;"/></th>
    <th><input size="30" type="text" id="actividade" onkeyup="filtertable('actividade')" style="border:1px dashed black;" /></th>
    <th><input size="50" type="text" id="email" onkeyup="filtertable('email')" style="border:1px dashed black;" /></th>
    </tr>

None of this works out of firefox.In internet explorer i get the error in all of them i get this error:

is not defined or is null.

in filter table the error is on :

document.getElementById('tbl1').innerHTML=xmlhttp.responseText; 

according to IE, and in toggle i get the error when i call the function in

onkeyup="filtertable('empresa')"

Can anybody explain to me why this is happening?

Upvotes: 1

Views: 184

Answers (1)

Louis Ricci
Louis Ricci

Reputation: 21086

This is the major reason people use a JavaScript framework like jQuery.

http://jquery.com/

Basic functionality like .getElementsByname, for each, checkbox.checked is normalized in these frameworks for cross browser compatibility.

Upvotes: 2

Related Questions