1

I'm creating a script to check something in my code. This script runs successfully on Chrome, but it doesn't run on IE.

This is run on onsubmit in my form

<form id="frm1" name="frm1" method="post" onsubmit="return checkForm();" action="save_dept_add.php">

And this is my script (I've put it in head tag)

<script language="javascript" type="text/javascript">
function checkForm()
{
        if (!window.console) {
             console = { log: function(){} };
        }
        var e = document.getElementById("dept");
        var xtext = e.options[e.selectedIndex].text;
        if(xtext == ""){
            console.log("no dept");
            alert("Please select your department");                 
            return false;

        }else{
            console.log(xtext);
            var q = document.getElementById("quantity");
            var check = q.value;
            console.log(check);
            if(check == ""){
                alert("Please insert a quantity");      
                return false;
            }else{
                return true;    
            }       
        }

}

Can anyone tell me what is wrong?

ChrisW
  • 4,970
  • 7
  • 55
  • 92
user3001046
  • 235
  • 1
  • 10
  • 28
  • what version of IE are you testing with? are you aware old versions of IE (<9) doesn't have console.log. and putting the rest of your form might help to see if there is any element-id related issues – DevZer0 Apr 29 '15 at 10:36
  • your code maybe failing in console.log since console object is not available in IE8 unless dev tools are open. – DevZer0 Apr 29 '15 at 10:39
  • http://stackoverflow.com/questions/7742781/why-javascript-only-works-after-opening-developer-tools-in-ie-once – DevZer0 Apr 29 '15 at 10:40
  • @DevZer0 i'm try to use develop tools in IE browser and start debug script pop-up been run , But when i'm not use debug for develop tools pop-up can't run. – user3001046 Apr 29 '15 at 10:40
  • see the link i posted, and the solution posted by Halayem bottom would fix your problem, but i would just get rid of that nasty alert. – DevZer0 Apr 29 '15 at 10:41
  • console.log is not available is IE8 which is why your code use failing – Mike Hamilton Apr 29 '15 at 11:42

1 Answers1

1

Add this to the top of your JavaScript file

if (!window.console) {
   console = { log: function(){} };
}
DevZer0
  • 13,433
  • 7
  • 27
  • 51
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45