简体   繁体   中英

Problem About HTML Code Created By Javascript Code

I have a javascript function which get datagrid's header into THEAD tags. This code triggered on body onload event and run correctly.

However, when i look to view source, i can't see these dynamicly generated codes. I want to see it there. How can I do it?

function AddTHEAD(tableName)
{
   var table = document.getElementById(tableName); 
   if(table != null) 
   {
    var head = document.createElement("THEAD");
    head.style.display = "table-header-group";
    head.appendChild(table.rows[0]);
    table.insertBefore(head, table.childNodes[0]); 
   }
}

Edit: I want to talk about why I need this: With the function above, I want Datagrid's header to be shown on every page when I print the page.

For this purpose I have css code below. When I add THEAD tag manually, it works fine. But when it's created by Javascript, it's seen on print preview but not on real printing.

    tHead
   {
    display : table-header-group;
   }

Edit2: I found a clue. When I firstly open print preview page and then click the print button there, it works. But when I directly clik the print button which run window.print javascript code, it don't works. Can anyone has a solution about it?

You won't be able to see javascript-generated code with 'view source'. This is because you are viewing the source generated by the webserver, and the HTML code you're looking for is generated by client-side javascript, after the webserver has served the HTML.

If you want to see the code generated by javascript, install Firebug for Firefox, and use the "Inspect Element" feature (right click on an element on the screen).

The source is now available in variable source .

var source = document.getElementById(tableName).innerHTML;
// show the source
alert(source);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM