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

161
Visualizações
Site shows without CSS for like 500ms after I added a random css script

I have added a javascript which loads a random css each time the site loads, and after I added this script, the website shows without CSS for a few milliseconds before showing normally

Here is the script:

(function() {
var sheet = document.createElement('link');
sheet.rel  = 'stylesheet';
sheet.href = 'css/' + (Math.floor(Math.random()*6)+1) + '.css';
document.getElementsByTagName('head')[0].appendChild(sheet);
})();

and here is where I put it in html I have tried putting it outside the body and outside the head but still the same issue

<html lang="en" class="no-js">
<head>
    <script type="text/javascript" src="js/random.js"></script>
<head>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Fixed new code:

var RANDOM_CSS = {
    cssfiles : ['1.css','2.css','3.css','4.css','5.css','6.css'],
    pathtocss : 'css/',
    getrandomcss : function() { return this.cssfiles[Math.floor(Math.random()*this.cssfiles.length)]; },
    getlinktag : function() { return '<link rel="stylesheet" type="text/css" href="'+this.pathtocss+this.getrandomcss()+'" />'; },
    printlinktag : function() { document.write(this.getlinktag()); }
  };

and

<script type="text/javascript" src="/js/random.js"></script>
<script type="text/javascript">
RANDOM_CSS.printlinktag();
</script>
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