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

359
Visualizações
My Javascript doesn't seem to be working - could the DOM have changed?

Like many of us, I want to keep my code as DRY(Don't Repeat Yourself) as possible - and that includes my website. I would like to do this by importing the common footer onto all pages - which is fine, and I'm doing this as follows:

page.html

<html> 
  <head> 
    <script type="text/javascript"> 
    $(function(){
      $("#footer").load("./Components/footer.html");
    });
    </script> 
  </head> 

  <body>
    This is my main page content
     
    <div id="footer"></div>  
  </body> 
</html>

footer.html

<div class="copyright-notice">
  <p align=center>
    Copyright © 2019 - <class id="getyear"></class> Joe Bloggs.
  </p>
</div>
<script>
  let d = new Date();
  document.getElementById("getyear").innerHTML = (new Date()).getFullYear();
</script>

The issue here the footer gets included - but it only contains the following:

Copyright © 2019 - Joe Bloggs.

I expect it to contain:

Copyright © 2019 - 2021 Joe Bloggs.

I'm wondering if it's a DOM problem. I can see, by the way, that the JavaScript is being executed - because if I put <class id="getyear"></class> into page.html then the JavaScript from footer.html works and puts the date in correctly.

It's a bit of a poser. Any ideas?

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

0

Your code is working properly :)

This is the screenshot enter image description here

page.html

<html>
  <head>
    <script
      src="https://code.jquery.com/jquery-3.6.0.min.js"
      integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
      crossorigin="anonymous"
    ></script>
    <script type="text/javascript">
      $(function () {
        $("#footer").load("./footer.html");
      });
    </script>
  </head>
  <body>
    This is my main page content
    <div id="footer"></div>
  </body>
</html>

footer.html

<div class="copyright-notice">
  <p align="center">
    Copyright © 2019 - <class id="getyear"></class> Joe Bloggs.
  </p>
</div>
<script>
  let d = new Date();
  document.getElementById("getyear").innerHTML = new Date().getFullYear();
</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