Javascript IE problem "unknown runtime error" when using innerHTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    Javascript IE problem "unknown runtime error" when using innerHTML

    Hi Everyone,

    I'm having this extremely annoying problem with Internet Explorer 6,
    giving me an error message saying "unknown runtime error" whenever I
    try to alter the contents of a <divelement using innerHTML.

    Now, I've researched this problem on the web, and found many references
    to it, but none of them quite addressed my specific situation, and
    since my experience with JavaScript is limited, I was not able to adapt
    the solutions I found to my specific situation.

    Anyway, it's all very basic:

    I have a <div id="myDiv"></divelement that's inside a <form></form>
    element. I'm trying to change the innerHTML of the <divelement when
    clicking on a "radio" button.

    This code works fine in Firefox:

    <html>
    <head>
    <title>My Page</title>

    <script type="text/javascript">
    function ChangeContent(s tr)
    {
    var obj=document.ge tElementById("m yDiv");

    if (str=="display1 ")
    {
    obj.innerHTML = "<b>display 1 was selected</b>";
    }
    else if (str=="display2 ")
    {
    obj.innerHTML = "<b>display 2 was selected</b>";
    }
    }
    </script>

    </head>

    <body>


    <form>
    Display1 <input type="radio" name="States" value="display1 "
    onchange="Chang eContent(this.v alue)" />
    <br />
    Display2 <input type="radio" name="States" value="display2 "
    onchange="Chang eContent(this.v alue)" />

    <div id="myDiv"></div>
    </form>


    </body>

    </html>

    As I said, it works perfectly in Firefox, but in IE6 I get the error:
    "unknown runtime error"
    >From what I could gather on the web, this is probably some kind of a
    "parent" "child" problem, since the <divelement is inside of a form..
    however since as I've mentioned my experience with JavaScript is
    limited, I need a practical example of how I can solve this issue for
    my particular situation...

    Any help will be greatly appreciated!!!! !

  • ASM

    #2
    Re: Javascript IE problem &quot;unknow n runtime error&quot; when using innerHTML

    John a écrit :

    I haven't IE, but you could try :
    <html>
    <head>
    <title>My Page</title>
    >
    <script type="text/javascript">
    function ChangeContent(s tr)
    {
    var obj=document.ge tElementById("m yDiv");
    if(str.selected )
    obj.innerHTML = (str.value=="di splay1")?
    "<b>display 1 was selected<\/b>" :
    (str.value=="di splay2")?
    "<b>display 2 was selected<\/b>" :
    "";
    }
    </script>
    >
    </head>
    >
    <body>
    <form>
    Display1 <input type="radio" name="States" value="display1 "
    onclick="Change Content(this)" />
    <br />
    Display2 <input type="radio" name="States" value="display2 "
    onclick="Change Content(this)" />

    <div id="myDiv"></div>
    </form>
    </body>
    >
    </html>
    >
    As I said, it works perfectly in Firefox, but in IE6 I get the error:
    "unknown runtime error"
    Probably because you didn't escape / of </bin your innerHTML ?
    >>From what I could gather on the web, this is probably some kind of a
    "parent" "child" problem, since the <divelement is inside of a form.
    I hope it is not that.

    On my idea it is curious to use onchange about a radio-button or a
    checkbox where no content can change ...


    --
    Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    Stephane Moriaux and his (less) old Mac already out of date

    Comment

    • Shinya Koizumi

      #3
      Re: Javascript IE problem &quot;unknow n runtime error&quot; when using innerHTML

      with my ie i don't get any error message.
      i am using ie6, too. i haven't apply the sp yet.


      sk


      Comment

      • dd

        #4
        Re: Javascript IE problem &quot;unknow n runtime error&quot; when using innerHTML

        I've been programming JS and primarily testing it first on IE6 for
        years
        and have never seen an "unknown runtime error". Have you tried it on
        other machines?

        I have a suggestion for you though. You're not checking whether
        the obj exists after your getElementById. It's always good practice
        to do this construct:

        var obj=document.ge tElementById(id );
        if(obj){
        //do stuff with obj
        }

        During testing you could have it alert that the object doesn't exist.

        else alert("obj didn't exist with id=="+id);

        Perhaps that's part of your problem, perhaps not. You'd normally
        see an error that it doesn't exist, rather than an unknown runtime
        error. Considering I've never seen such a thing though, I thought
        I'd bring this to your attention. You never know.

        Comment

        • Zeraxp

          #5
          Re: Javascript IE problem &quot;unknow n runtime error&quot; when using innerHTML

          On Jan 13, 6:36 pm, "John" <quanza....@gma il.comwrote:
          Hi Everyone,
          >
          I'm having this extremely annoying problem with Internet Explorer 6,
          giving me an error message saying "unknown runtime error" whenever I
          try to alter the contents of a <divelement using innerHTML.
          >
          Now, I've researched this problem on the web, and found many references
          to it, but none of them quite addressed my specific situation, and
          since my experience with JavaScript is limited, I was not able to adapt
          the solutions I found to my specific situation.
          >
          Anyway, it's all very basic:
          >
          I have a <div id="myDiv"></divelement that's inside a <form></form>
          element. I'm trying to change the innerHTML of the <divelement when
          clicking on a "radio" button.
          >
          This code works fine in Firefox:
          >
          <html>
          <head>
          <title>My Page</title>
          >
          <script type="text/javascript">
          function ChangeContent(s tr)
          {
          var obj=document.ge tElementById("m yDiv");
          >
          if (str=="display1 ")
          {
          obj.innerHTML = "<b>display 1 was selected</b>";
          }
          else if (str=="display2 ")
          {
          obj.innerHTML = "<b>display 2 was selected</b>";
          }}
          >
          </script>
          >
          </head>
          >
          <body>
          >
          <form>
          Display1 <input type="radio" name="States" value="display1 "
          onchange="Chang eContent(this.v alue)" />
          <br />
          Display2 <input type="radio" name="States" value="display2 "
          onchange="Chang eContent(this.v alue)" />
          >
          <div id="myDiv"></div>
          </form>
          >
          </body>
          >
          </html>
          >
          As I said, it works perfectly in Firefox, but in IE6 I get the error:
          "unknown runtime error"
          >
          From what I could gather on the web, this is probably some kind of a
          >
          "parent" "child" problem, since the <divelement is inside of a form..
          however since as I've mentioned my experience with JavaScript is
          limited, I need a practical example of how I can solve this issue for
          my particular situation...
          >
          Any help will be greatly appreciated!!!! !

          you had to try with a simple javascript string like "ok" or "not ok"
          and your code should work but with html tags just need to escape / in
          javascript strings !!!

          your code modified :

          <script type="text/javascript">
          function ChangeContent(s tr)
          {
          var obj=document.ge tElementById("m yDiv");

          if (str=="display1 ")
          {
          obj.innerHTML = "<b>display 1 was selected<\/b>";
          }
          else if (str=="display2 ")
          {
          obj.innerHTML = "<b>display 2 was selected<\/b>";
          }
          }

          </script>

          Comment

          • matt.mmorgan@gmail.com

            #6
            Re: Javascript IE problem &quot;unknow n runtime error&quot; when using innerHTML

            This code works fine in Firefox:
            <script type="text/javascript">
            function ChangeContent(s tr)
            {
            var obj=document.ge tElementById("m yDiv");
            >
            if (str=="display1 ")
            {
            obj.innerHTML = "<b>display 1 was selected</b>";
            }
            else if (str=="display2 ")
            {
            obj.innerHTML = "<b>display 2 was selected</b>";
            }}
            >
            </script>
            The reason this code works in Firefox and not IE is due to your
            calling of document.getEle mentByID("myDiv "); This method is the open
            standards way of pulling objects in the DOM, but IE does not follow
            this rule. Adding this check should allow it to work for both IE and
            Firefox:

            if(document.get ElementByID) { //Open standards method
            var obj=document.ge tElementById("m yDiv");
            }
            else if(document.all ) { //IE method
            var obj=document.al l['myDiv']; //Note the brackets rather than
            parenthesis
            }

            Comment

            • VK

              #7
              Re: Javascript IE problem &quot;unknow n runtime error&quot; when using innerHTML

              On Jan 13, 8:36 pm, "John" <quanza....@gma il.comwrote:
              Hi Everyone,
              >
              I'm having this extremely annoying problem with Internet Explorer 6,
              giving me an error message saying "unknown runtime error" whenever I
              try to alter the contents of a <divelement using innerHTML.
              >
              Now, I've researched this problem on the web, and found many references
              to it, but none of them quite addressed my specific situation, and
              since my experience with JavaScript is limited, I was not able to adapt
              the solutions I found to my specific situation.
              >
              Anyway, it's all very basic:
              >
              I have a <div id="myDiv"></divelement that's inside a <form></form>
              element. I'm trying to change the innerHTML of the <divelement when
              clicking on a "radio" button.
              >
              This code works fine in Firefox:
              >
              <html>
              <head>
              <title>My Page</title>
              >
              <script type="text/javascript">
              function ChangeContent(s tr)
              {
              var obj=document.ge tElementById("m yDiv");
              >
              if (str=="display1 ")
              {
              obj.innerHTML = "<b>display 1 was selected</b>";
              }
              else if (str=="display2 ")
              {
              obj.innerHTML = "<b>display 2 was selected</b>";
              }}
              >
              </script>
              >
              </head>
              >
              <body>
              >
              <form>
              Display1 <input type="radio" name="States" value="display1 "
              onchange="Chang eContent(this.v alue)" />
              <br />
              Display2 <input type="radio" name="States" value="display2 "
              onchange="Chang eContent(this.v alue)" />
              >
              <div id="myDiv"></div>
              </form>
              >
              </body>
              >
              </html>
              >
              As I said, it works perfectly in Firefox, but in IE6 I get the error:
              "unknown runtime error"
              Yeah. And the googled coverage is rather amazing:
              <http://www.google.com/search?hl=en&q...error+Internet
              +Explorer&btnG= Google+Search>
              So many different explanations (some are stated as the ultimate truth
              atop of it) and not a single correct one.

              The problem is that the code you have posted is not the code you have
              troubles with. The actual code would look something like here <http://
              piecesofrakesh. blogspot.com/2007/02/ies-unknown-runtime-error-when-
              using.html>

              Try to notice the principal difference. If no luck then it is time to
              learn the "VK's mantra" I'm too lazy to search a post, easier to type
              in as it's very short and simple:

              Before page load event document.write only
              Ohmmm...
              After page load event DOM methods only
              Ohmmm...

              ;-)

              Comment

              • Duncan Booth

                #8
                Re: Javascript IE problem &quot;unknow n runtime error&quot; when using innerHTML

                matt.mmorgan@gm ail.com wrote:
                The reason this code works in Firefox and not IE is due to your
                calling of document.getEle mentByID("myDiv "); This method is the open
                standards way of pulling objects in the DOM, but IE does not follow
                this rule.
                Rubbish. Of course IE supports getElementById.

                Comment

                Working...