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

137
Visualizações
javascript function works only on firefox

I made a function that shows card images after I click a button. the problem is that it doesn't work on chrome. the chrome editor presents an extra '/' to the end of the path.

        function renderDeck(deck,ph)
    {
        
        var htmlStr = document.getElementById(ph).innerHTML;
        for (var i = 0; i < deck.length; i++) {
            htmlStr += '<div>';
            htmlStr += '<img src=' + deck[i].path + '/>';
            htmlStr += '</div>';
        }
        document.getElementById(ph).innerHTML = htmlStr;
    }

An example of what I push inside the deck

deck.push({ name: 'red_joker', path: 'cardImages/red_joker.png', val: 15 });

what can be the problem?

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

0

Off Topic: Avoid innerHTML += ... because

  • it has to serialize and then parse all existing dom nodes
  • it replaces all existing nodes with copies/new ones, so things like event handler, references you've kept, all will be gone. Everything that is not expressed in the markup itself.
function renderDeck(deck, ph) {
  // create the markup for the new items 
  const htmlStr = deck.map(item => `<div><img src="${item.path}"></div>`).join("\n");

  // parse the `htmlStr` into a DOM Node that you can add to other DOM Nodes. 
  const fragment = document.createRange().createContextualFragment(htmlStr);

  // append the DocumentFragment to the existing element.
  document.getElementById(ph).append(fragment);
}

And a link for those that don't know yet what a DocumentFragment is.

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