Javascript IE problem "unknown runtime error" when using innerHTML

Discussion in 'Javascript' started by John, Jan 13, 2007.

  1. John

    John Guest

    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 <div> element 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"></div> element that's inside a <form></form>
    element. I'm trying to change the innerHTML of the <div> element when
    clicking on a "radio" button.

    This code works fine in Firefox:

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

    <script type="text/javascript">
    function ChangeContent(str)
    {
    var obj=document.getElementById("myDiv");

    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="ChangeContent(this.value)" />
    <br />
    Display2 <input type="radio" name="States" value="display2"
    onchange="ChangeContent(this.value)" />

    <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 <div> element 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!!!!!
     
    John, Jan 13, 2007
    #1
    1. Advertising

  2. John

    ASM Guest

    John a écrit :

    I haven't IE, but you could try :

    > <html>
    > <head>
    > <title>My Page</title>
    >
    > <script type="text/javascript">
    > function ChangeContent(str)
    > {
    > var obj=document.getElementById("myDiv");


    if(str.selected)
    obj.innerHTML = (str.value=="display1")?
    "<b>display 1 was selected<\/b>" :
    (str.value=="display2")?
    "<b>display 2 was selected<\/b>" :
    "";

    > }
    > </script>
    >
    > </head>
    >
    > <body>


    <form>
    Display1 <input type="radio" name="States" value="display1"
    onclick="ChangeContent(this)" />
    <br />
    Display2 <input type="radio" name="States" value="display2"
    onclick="ChangeContent(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 </b> in your innerHTML ?

    >>From what I could gather on the web, this is probably some kind of a

    > "parent" "child" problem, since the <div> element 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
     
    ASM, Jan 14, 2007
    #2
    1. Advertising

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


    sk
     
    Shinya Koizumi, Jan 14, 2007
    #3
  4. John

    dd Guest

    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.getElementById(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.
     
    dd, Jan 16, 2007
    #4
  5. John

    Zeraxp Guest

    On Jan 13, 6:36 pm, "John" <> wrote:
    > 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 <div> element 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"></div> element that's inside a <form></form>
    > element. I'm trying to change the innerHTML of the <div> element when
    > clicking on a "radio" button.
    >
    > This code works fine in Firefox:
    >
    > <html>
    > <head>
    > <title>My Page</title>
    >
    > <script type="text/javascript">
    > function ChangeContent(str)
    > {
    > var obj=document.getElementById("myDiv");
    >
    > 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="ChangeContent(this.value)" />
    > <br />
    > Display2 <input type="radio" name="States" value="display2"
    > onchange="ChangeContent(this.value)" />
    >
    > <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 <div> element 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(str)
    {
    var obj=document.getElementById("myDiv");

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

    </script>
     
    Zeraxp, Feb 7, 2007
    #5
  6. John

    Guest

    > This code works fine in Firefox:
    > <script type="text/javascript">
    > function ChangeContent(str)
    > {
    > var obj=document.getElementById("myDiv");
    >
    > 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.getElementByID("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.getElementByID) { //Open standards method
    var obj=document.getElementById("myDiv");
    }
    else if(document.all) { //IE method
    var obj=document.all['myDiv']; //Note the brackets rather than
    parenthesis
    }
     
    , Feb 26, 2007
    #6
  7. John

    VK Guest

    On Jan 13, 8:36 pm, "John" <> wrote:
    > 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 <div> element 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"></div> element that's inside a <form></form>
    > element. I'm trying to change the innerHTML of the <div> element when
    > clicking on a "radio" button.
    >
    > This code works fine in Firefox:
    >
    > <html>
    > <head>
    > <title>My Page</title>
    >
    > <script type="text/javascript">
    > function ChangeContent(str)
    > {
    > var obj=document.getElementById("myDiv");
    >
    > 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="ChangeContent(this.value)" />
    > <br />
    > Display2 <input type="radio" name="States" value="display2"
    > onchange="ChangeContent(this.value)" />
    >
    > <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=unknown runtime 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...

    ;-)
     
    VK, Feb 26, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Debono

    ADOX... Unknown runtime error

    Ivan Debono, Oct 30, 2004, in forum: ASP General
    Replies:
    7
    Views:
    179
    Andre Kemper
    Nov 1, 2004
  2. RyanFoushee

    Unknown runtime error

    RyanFoushee, Aug 12, 2004, in forum: Javascript
    Replies:
    0
    Views:
    83
    RyanFoushee
    Aug 12, 2004
  3. Replies:
    8
    Views:
    238
    Thomas 'PointedEars' Lahn
    Nov 23, 2005
  4. sonic
    Replies:
    5
    Views:
    294
    Randy Webb
    Jul 11, 2006
  5. unknown runtime error in IE

    , May 26, 2008, in forum: Javascript
    Replies:
    3
    Views:
    111
    Thomas 'PointedEars' Lahn
    May 27, 2008
Loading...

Share This Page