unknow error in MSIE while using innerHTML property

Discussion in 'Javascript' started by zero0x, Jan 5, 2007.

  1. zero0x

    zero0x Guest

    hi all,

    i'm trying to create something like window in javascript.

    there is no problem in firefox, but in microsoft internet explorer i
    get "Unknown error while working" (this message is in slovak language,
    and i has translated it, so maybe in english version it differs). when
    i comment this line:

    tab.innerHTML="test";

    everything works ok. i have really no idea, what could be wrong.


    here is the code listing

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;
    charset=windows-1250">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <script>
    function center(obj)
    {
    //function that centers object obj
    owidth=obj.offsetWidth;
    oheight=obj.offsetHeight;

    pwidth=document.body.clientWidth;
    pheight=document.body.clientHeight;

    pleft=(pwidth-owidth)/2;
    ptop=(pheight-oheight)/2;

    //presunut objekt
    cleft=pleft+"px";
    ctop=ptop+"px";


    obj.style.position="absolute";
    obj.style.left=cleft;
    obj.style.top=ctop;
    }
    function nwin(text)
    {
    var okinko="<tr><td width=\"15\" height=\"17\"
    background=\"images/panel_03.jpg\"></td><td height=\"17\"
    background=\"images/panel_04.jpg\"></td><td width=\"15\" height=\"17\"
    background=\"images/panel_05.jpg\"></td></tr><tr><td width=\"15\"
    background=\"images/panel_07.jpg\"></td><td
    background=\"images/panel_08.jpg\">"+text+"</td><td width=\"15\"
    background=\"images/panel_09.jpg\"></td></tr><tr><td width=\"15\"
    height=\"16\" background=\"images/panel_11.jpg\"></td><td height=\"16\"
    background=\"images/panel_12.jpg\"></td><td width=\"15\" height=\"16\"
    background=\"images/panel_13.jpg\"></td></tr>"; //this variable
    contains everything inside table
    tab=document.createElement("table"); //i create the table
    tab.innerHTML=okinko; // !!! here is the problem !!!
    tab.setAttribute("cellspacing",0);
    document.body.appendChild(tab); //append table to body
    center(tab); //this function put my table into center of document
    }
    </script>
    </head>
    <body>
    <a href="#"
    onclick="nwin('aaaaaaaaaaaa<br>aaaaaaaaaa<br>aaaaaaaaaaaaaaa');return
    false;">aaa</a>
    <hr />

    </body>
    </html>
     
    zero0x, Jan 5, 2007
    #1
    1. Advertisements

  2. Create the table (table element, tbody element, tr, td elements)
    completely with createElement. IE does not allow you to set innerHTML on
    a table element.

    Or create the complete table HTML with innerHTML e.g.
    var div = document.createElement('div');
    div.innerHTML = '<table cellspacing="0">' + okinko + '<\/table>';
    then you can do e.g.
    document.body.appendChild(div.firstChild);
     
    Martin Honnen, Jan 5, 2007
    #2
    1. Advertisements

  3. zero0x

    RobG Guest

    Martin has shown you the error, I'll just add that rather than
    createElement, you can also use the more conscise insertRow and
    insertCell methods to create the table using DOM:

    var numRows = 4;
    var numCells = 6;
    var row, cell;
    var oTable = document.createElement('table');

    for (var i=0; i<numRows; i++){
    row = oTable.insertRow(-1);
    for (var j=0; j<numCells; j++){
    cell = row.insertCell(-1);

    /* Insert cell content here */

    }
    }
    document.body.appendChild(oTable);


    insertCell:
    <URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-68927016 >

    insertRow:
    <URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903 >
     
    RobG, Jan 5, 2007
    #3
  4. zero0x

    zero0x Guest

    thank you,

    i didnt know that msie has this (maybe) bug

    i like the both solutions, but i think it is simplier to do it without
    insertRow and insertCell.

    vojto
     
    zero0x, Jan 6, 2007
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.