views:

196

answers:

3

Here's my problem: there's an internal issue tracking system that has a nice summary page (number of open issues broken down by developer, etc.) However, the compact summary table is surrounded by boilerplate cruft (navigational menu, search box, etc.) I'd like to embed the summary table on our team's page, but not the surrounding cruft. Using an <iframe> tag would be a trivial solution, if it were not for the cruft around the table I'm interested in. Luckily, the HTML table in question is well-delineated: it is defined with a regular <table> tag and it even has a unique id assigned to it. I was wondering if there was a simple JavaScript trick involving DOM manipulation that I could use to surgically extract the table I'm interested in within the iframe. That way, the report would always be up to date whenever somebody checks our team's homepage.

A: 

Something simple for you to expand on?

function scrapeTable(tableId)
  {
   var tables = document.getElementsByTagName("table");
   for (var i = 0; i < tables.length; i++)
   {
    if (tables[i].id == tableId)
    {
     alert("<table>" + tables[i].innerHTML + "</table>");
    }
   }
  }
A S
apologies for the formatting...
A S
That works, but the "innerHTML()" of the table won't include the table tags themselves. That's probably not a huge deal provided that browsers are OK with dropping table guts into a newly-built (empty) table.
Pointy
+1  A: 

If you use jQuery, you can pull the table code pretty easily. Now if the table contains little script blocks, things might get weird; similarly, if it relies on some stylesheets, make sure your destination page has those too.

Is the table surrounded by some sort of block-level element (a or something)? If so, then you can load the page (AJAX or hidden iframe), find the table by "id", traverse to the parent, and then move its html() value to your destination block. If there's no block-level parent suitable for that, then you can try reconstructing an empty table tag at your destination and then pull the html() from the source table and drop it there; however, things start getting a little weird sometimes because some browsers handle table layout in funny ways. Might work however.

Pointy
This is sort of what I did, with the additional trick of uploading an attachment to the issue tracker so that my JavaScript would be served from the same domain as the summary report.
Lajos Nagy
+1  A: 

Is the team and issue tracker on the same domain/address?

If it isn't then your probably going to have to modify the issue tracker to get the desired result. Most browsers wont let you modify/access across different domains for security reasons.

If they are on the same domain then you could probably use jQuery's load with a selector:

$("#destination").load("/path/to/table/ #tableid");
envalid
yea that's a really good idea - I forgot about the selector trick with load().
Pointy