Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Javascript not working in IE

    I'm launching the below javascript with an onChange event in the html:

    <select name="date" onChange="showUser(this.value)">

    I've verified that the onChange event is getting to the javascript by doing a simple alert("working"); and IE did fire up the alert, however the actual code that I want does not seem to work. (its functional in FF, Chrome, Safari, iPad, and Android).

    What the function does is: take the value passed to it through the onchange, and send it to a php file as a variable. The php file takes the variable it gets and inserts it into a DB. I don't think the php portion is the problem because it works in every other browser. Does anyone have any insight?



    Code:
    function showUser(str)
    {
    if (str=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      } 
    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("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","http://testest.com/itgoeshere.php?q="+str,true);
    xmlhttp.send();
    }

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    If you have not explicitly added value attributes to each <option> tag, you must do so for I.E.

  • #3
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    If you have not explicitly added value attributes to each <option> tag, you must do so for I.E.
    Here's an example for what the select statement looks like, so they definitely all have values assigned.


    <select name="date" onChange="showUser(this.value)">
    <option value="">---</option>
    <option value="1">Past hour</option>
    <option value="2">Past week</option>
    <option value="3">Past month</option>
    <option value="4">Past year</option>
    </select>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    <select name="date" onchange="showUser(this.value)">
    <option value="">---</option>
    <option value="1">Past hour</option>
    <option value="2">Past week</option>
    <option value="3">Past month</option>
    <option value="4">Past year</option>
    </select>
    
    <script type = "text/javascript">
    function showUser(which) {
    alert (which);
    }
    
    </script>
    Works fine for me in all browsers.

    "I am not a vegetarian because I love animals; I am a vegetarian because I hate plants." - A. Whitney Brown.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,978
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Code looks right to me, and I have used nearly identical code in IE (and all other browsers) many times.

    But you could (a) use the MSIE debugger and/or (b) put in a bunch more alerts to see where you are getting to.

    Example:
    Code:
    function showUser(str)
    {
        alert("showUser(" + str + ") was called");
        if (str=="")
        {
            document.getElementById("txtHint").innerHTML="";
            return;
        } 
        if (window.XMLHttpRequest)
        { // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
            alert("using XMLHttpRequest");
        } else { // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            alert("using ActiveXObject");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                alert("got response");
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","http://testest.com/itgoeshere.php?q="+str,true);
        alert("opened xmlhttp");
        try { 
            xmlhttp.send();
            alert("called send");
        } catch ( e ) {
            alert"error in send " + e.toString() );
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    phpchick (07-13-2012)

  • #6
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Good idea, pendant, trying that now.

  • #7
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    With the code you provided, it completely bombs out. No alerts, no anything.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,978
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    A typo.
    Code:
    alert"error in send " + e.toString() );
    Missing the initial (
    Code:
    alert("error in send " + e.toString() );
    If you would use the error console in ANY browser it would tell you about that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,978
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Quote Originally Posted by phpchick View Post
    Good idea, pendant, trying that now.
    Why does everybody thing I'm a dangling object?
    http://dictionary.reference.com/browse/pedantic

    Definition 2 fits. You *need* to be "overly concerned with minute details or formalisms" when you are coding.
    Last edited by Old Pedant; 07-13-2012 at 09:28 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Why does everybody thing I'm a dangling object?

    http://en.wikipedia.org/wiki/Pedant


    Haha, sorry. Yeah, I caught that error, sorry about that. debugging now...

  • #11
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Why does everybody thing I'm a dangling object?
    http://dictionary.reference.com/browse/pedantic

    Definition 2 fits. You *need* to be "overly concerned with minute details or formalisms" when you are coding.



    Mr. Pedant You are never going to believe this.

    So I added the alerts and the code started working in IE.

    I then commented out the alerts, and it continued to work, so I guess that's our answer?


    I have no idea how that works? How could the addition of the alerts have solved it?

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,978
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    I would *GUESS* the IE was caching an old version of your page when you had the problem before. But that's just a guess.

    As I said, the code looked right to me the first time around.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •