Data js in html(external script)

Asked

Viewed 170 times

0

I have the following script that returns date and time:

function RetornaDataHoraAtual(){
    var dNow = new Date();
    var localdate = dNow.getDate() + '/' + (dNow.getMonth()+1) + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
    return localdate;
 }

And an html page (the following) where I call it, in this code snippet (which, when I run, nothing happens - no output is displayed). Where can I be missing?

<div class="container">
  <label>Cronômetro de Monitoramento, até a data de  <script src="retornaData.js"></script> </label>
    <?!= include("grafico"); ?>
    <font size="1" face="Verdana"><i><b>Nota:</b> Apenas monitoramentos concluídos(no prazo e fora do prazo) e empacados. </i><u>Excluem-se os que estão em andamento.</u></i></fonte>
  </div>

  <div class="row">
  • If I am not mistaken, it is necessary to initialize the function: 'nameFuncao()', it seems that you are only declaring the function inside the script, you need to press play. I could be wrong.

  • But when I call the function, inside it, I already have the Return, which returns the variable that stores the date.

  • The correct is after the statement, no?

1 answer

1


I believe that this is not the best way to use this function. You can use the options of Document.getElementById to capture the element where you want to leave your text, the Document.createTextNode to create the text element and the appendchild to secure it in the chosen element.

(You can put the external javascript, I’m just demonstrating in the middle of the code)

Follow the example below:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<div class="container">
  <label>Cronômetro de Monitoramento, até a data de  <span id="data"></span></label>
    <?!= include("grafico"); ?>
    <font size="1" face="Verdana"><i><b>Nota:</b> Apenas monitoramentos concluídos(no prazo e fora do prazo) e empacados. </i><u>Excluem-se os que estão em andamento.</u></i></fonte>
  </div>

  <div class="row">


<script>

let data = document.getElementById('data');
var text = document.createTextNode(RetornaDataHoraAtual());        
data.appendChild(text);

function RetornaDataHoraAtual(){
    var dNow = new Date();
    var localdate = dNow.getDate() + '/' + (dNow.getMonth()+1) + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
    return localdate;
 }
</script>
</body>
</html>

In case there are any questions, I’m willing to help you!

  • It worked. However, it does not run if the script is external. I don’t understand. But the way it is in the code is showing as I want.

  • 1

    Roger, all right? Are you entering the external file by tag? It would look like this: <script type="text/javascript" src="js/external-file-name.js"></script>

  • Hi, I’ll leave it the way it is, in the same code. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.