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

132
Visualizações
Execute *.js if month is December

Ok, I give up.

I'm trying to make this script

<script>
$(document).ready(function(){
    var d = new Date();
    n = d.getMonth();

    if (n == 11) {
       src="extrafiles/effect/snow.js";
    }
});
</script>

call "snow.js" whenever the month is december.

Won't work though. Ideas ?

EDIT:

Maybe this is a better approach ?

if ( d.getMonth().match(11) ) { //do call snow.js; }

EDIT2:

This works for determining season, tweakable down to the month

<script>
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var total = month;
if (total == 12 || total == 1 || total == 2)
{
//beginning of script
START JS FILE VIA ???
//end of script
}
</script>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try to do it like that :

  if (n === 11) {
    let myScript = document.createElement("script");
    myScript.setAttribute("src", "https://www.example.com/snow.js");
    document.body.appendChild(myScript);
    makeSnow();
  }

or in ES6 mode :

<!doctype html>
<script>
  async function load() {
    let say = await import('./snow.js');
    say.makeSnow();

  }
</script>
<button onclick="load()">Click me</button>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can make use of import to import the file based on your condition. You could also import the file anyway and just call your function from that file inside of your conditional.

<DOCTYPE html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
      $(async () => { // same as $(document).ready(async function(){
        const month = new Date().getMonth()
        if (month === 11) {
          const effect = await import('./snow.js')
          effect.run()
        }
      })
    </script>
</head>
<body>

</body>

This is an example of just importing the file at the start

<DOCTYPE html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
      const effect = await import('./snow.js')
      
      $(async () => { 
        const month = new Date().getMonth()
        if (month === 11) {
          effect.run()
        }
      })
    </script>
</head>
<body>

</body>

Note that this will throw an error on Stack Overflow because that file doesn't exist but I have verified that it works locally.

Since you've shown the file, I can see that it doesn't actually export anything so import won't work for you here. Instead, you're going to have to retroactively add the script to the DOM

<DOCTYPE html>
<head>
</head>
<body>
  <script>
      document.addEventListener('DOMContentLoaded', () => {
        const s = document.createElement('script')
        s.type = 'text/javascript'
        s.src = 'https://ry3yr.github.io/OSTR/myhomepage/snow.js'
        document.body.appendChild(s)
      })
  </script>
</body>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use document.write to add the script

if (new Date().getMonth()===11) document.write('<script src="https://ry3yr.github.io/OSTR/myhomepage/snow.js"><\/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