SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IE unknown runtime error with innerHTML

    Hi,

    So I've searched around on this error and there seem to be a lot of discussions, but I haven't been able to find an answer for my specific problem. So here's a basic example of what I'm trying to do, as the real one is long. The whole purpose of this was to avoid going to the database again.

    There is a page that loads an article from a Java bean (J2EE that is) with a headline and body property. So I've placed a span around the headline with an id and the same with the body, I think it's a div on the body, doesn't really matter, problem is the same. I wrote a javascript function to create a form, a text input for the headline, and a textarea for the body, all in a hidden div, and then the function submits the form to a popup and creates a printer friendly version of the page. I didn't want to have to retrieve the article from the database when I can just send it in the post. This works very well in Firefox, then I tried it in IE and got the very useful error "Unknown runtime error", thanks. Here's a small example that produces the same error.

    Code:
    <html>
        <head>
        <title>Test page</title>
        <script type="text/javascript">
        <!--
           
        function doSomethingWithInnerHTML() {
    
            var bodyText = document.createElement('textarea');
    
            bodyText.innerHTML = document.getElementById('helloWorld').innerHTML;
    
            document.getElementById('myDiv').appendChild(bodyText);
    
    
        }
    
        // -->
        </script>
        </head>
        <body>
            <span id="helloWorld">
            <h3>Hello</h3><h1>World</h1></span>
            <div id="myDiv" style="background:#00ff00;color:#ffffff">
                <a href="javascript:doSomethingWithInnerHTML()">innerHTML Test</a>
            </div>
        </body>
    </html>
    I've seen similar threads on this problem and the general solution is to wrap things in a div tag, but I had tried this which seems to work in IE but gives blank text area in Firefox. At this point I decided to cut my losses on this and just do it on the backend side of things which I far more control over. But I'd like to know if there is a good solution for this.

  2. #2
    Twitter - @CarlBeckel busy's Avatar
    Join Date
    May 2004
    Location
    Richmond, VA, USA
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of innerHTML try this
    Code:
    bodyText.value='whatever';
    I had this problem recently.

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You shouldn't put block level elements inside inline elements. So your span shouldn't contain h3 and h1. There should be no problem swapping your span for a div.

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just use CSS to alter the appearance of the page when it gets printed?

    <link rel="stylesheet" href="print.css" media="print">


Bookmarks

Posting Permissions

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