#How to display HTML Table that is inside a txt file in another HTML file

66 messages · Page 1 of 1 (latest)

sour zinc
#

I have 2 files, Main HTML and Output.txt which has my HTML table inside it, Is it possible to automatically display the table from the output.txt file to Main html using javascript?

lilac sable
#

yes you can but what kind of table? that's just like over complicating the process

#

you can use vanilla js using fetch function and then parse it to a div, or use jquery

#

lemme know which way to go, i'll help out

sour zinc
lilac sable
#

but you said you have a table in a txt file, right?

sour zinc
#

yes its okay txt file, i can also generate that table to be exported as .html file

#

so either txt or .html its fine with me

lilac sable
#

okay, here goes, i'll assume you're using Output.txt

#
fetch('Output.txt')
  .then(response => response.text())
  .then(data => {
    const parser = new DOMParser();
    const htmlTable = parser.parseFromString(data, 'text/html').querySelector('table');
    const container = document.querySelector('#table-container');
    container.appendChild(htmlTable);
  });

<div id="table-container"></div>
#

i think this shold work, basic domparser

sour zinc
#

okay ill test the code and see how it goes, Ill also send the full html file here for you to check it out

sour zinc
lilac sable
#

why dont you just use react that'll make things easy for u

sour zinc
#

It only displays the table when im using a live server

#

"127.0.0.1:5500/index.html"

#

if i open the html without a live server it will not display the table

lilac sable
#

that's because you're using fetch() and depends browser to browser how that function is handled

#

to fix it you'll need ajax or jquery but you said, you can't use that

sour zinc
#

is there another way? because the employees will not have a live server just click on the html to fetch it?

sour zinc
lilac sable
#

nope

#

what office doesnt have internet? but ig u can give xml a try

#
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (this.readyState === 4 && this.status === 200) {
    const parser = new DOMParser();
    const htmlTable = parser.parseFromString(this.responseText, 'text/html').querySelector('table');
    const container = document.querySelector('#table-container');
    container.appendChild(htmlTable);
  }
};
xhr.open('GET', 'Output.txt');
xhr.send();
sour zinc
lilac sable
lilac sable
sour zinc
#

darn, it didnt work

#

Is it possible to do it with xml?, but how would i structure the html table inside xml format?

lilac sable
#

leave xml, its of no use

sour zinc
#

okay

#

its weird because html iframe works between files without live server

lilac sable
#

idk man that's such a weird question to me

#

i work with mern stack, without internet i dont exist, so umm yeah fool your boss and use ajax

#

or ig you can download the jquery script, and import it locally

sour zinc
lilac sable
#

believe me you dont want to type it

lilac sable
sour zinc
# lilac sable how does that work in a company, you've given me something weird to think abt

So im not in the IT department, I work at a marketing department so obviously they don't use a "programming" language or tool and the IT won't provide one for me, So the team asked if i can do it using a notepad to write the whole thing, It works with static html pages but i want to extend it and make it automated for them because i have a made a VBA code in excel to export excel table as an HTML table and then "hopefully" display it and automate without the need for me to copy the table and pasted inside the .html file each time their is an update

sour zinc
# lilac sable

wait a minute !!!!! If i paste this code inside HTML file and put it inside a comment "/", will the IT antivirus be able to detect it?

#

at the time of injecting it inside the share point

lilac sable
#

why would you put a comment?

sour zinc
#

so that the antivirus doesnt read it as a "script"

#

running in a file etc

lilac sable
#

and an i.t. antivirus that's the first time i ever heard of it, we either bring our own macbooks or mac minis just connect em up to display and start working and chill, yours sounds like a strict company nvm

lilac sable
#

ask em to allow jquery it'll make the whole thing just a 1 line function

sour zinc
#

Thanks for the tip brother, Ill try for that 👍

sour zinc
# lilac sable

I downloaded the Jquery file, I wanna just test the process if you can continue helping

lilac sable
#
$(function() {
  $('#table-container').load('Output.txt');
});
#

after importing the file

sour zinc
#

is this the correct way?

lilac sable
#

yeah

sour zinc
#

it doesnt work without a live server

lilac sable
#

then my frnd there's no way

#

because all webpages are meant to be deployed to internet not a local machine without a server

sour zinc
#

We have a sharepoint

lilac sable
#

however if you're all on same network you can use node

lilac sable
sour zinc
sour zinc
lilac sable
#

i'll ask chatgpt to write the instructions, but basically yes