Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
CSV Filled Table (Via JS) Not Loading View In Browser Until Manual Refresh

So I'm really not a JS guy in the slightest, but wanted to basically render a CSV as table on my Jekyll site - I have this working with the following script.

Only problem is it never seems to load the table first time, I need to click refresh in the browser for it to appear - does anyone have any ideas as to what could be the cause of this?

<script>
    window.onload = function() {
        with(new XMLHttpRequest()) {
            onreadystatechange = cb;
            open('GET', 'https://raw.githubusercontent.com/clintjb/A350-Tracking/main/flight_data_a350.csv', true);
            responseType = 'text';
            send();
        }
    }

function cb() {
    if (this.readyState === 4) document.getElementById('A350')
        .innerHTML = tbl(this.responseText);
}

function tbl(csv) {
    return csv.split('\n')
        .map(function(tr, i) {
            return '<tr><td>' +
                tr.replace(/,/g,'</td><td>') +
                '</td></tr>';
        })
        .join('\n');
}
</script>

<table border="0" style='font-size:50%' id="A350"></table>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I was not able to recreate the issue on my machine. Your code worked fine for me, at least in chrome, but one thing you could try is using fetch instead of XMLHttpRequest. both worked for me

That would look something like the following

<script>
    fetch('https://raw.githubusercontent.com/clintjb/A350-Tracking/main/flight_data_a350.csv'
    ).then((response) => {
        return response.text();
    }).then((text) => {
        document.getElementById('A350').innerHTML = tbl(text);
    })

    function tbl(csv) {
        return csv.split('\n')
            .map(function (tr, i) {
                return '<tr><td>' +
                    tr.replace(/,/g, '</td><td>') +
                    '</td></tr>';
            })
            .join('\n');
    }

</script>

<table border="0" style='font-size:50%' id="A350"></table>
about 4 years ago · Juan Pablo Isaza Relatório

0

So all the solutions (including my first proposal) actually worked. The issue in the end was actually a strange bug in the template used on the Jekyll site.

If you went directly to the page it rendered fine, if you refreshed it rendered fine - was only going via the menus which would than cause the rendering issue.

Due to this I changed the approach and used a GitHub actions to push the file each day - with the CSV in the same repo as the (e.g. like in _data/my_file.csv) you can access the data when the site builds as

{{ site.data.my_csv }}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda