How to run external linked Javascript

Asked

Viewed 5,899 times

1

I own the Javascript date:

var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var timeValue = "" + (hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes + "h "
timerRunning = true;

mydate = new Date();
myday = mydate.getDay();
mymonth = mydate.getMonth();
myweekday= mydate.getDate();
weekday= myweekday;
myyear= mydate.getFullYear();
year = myyear

if(myday == 0)
day = " domingo, "

else if(myday == 1)
day = " segunda-feira, "

else if(myday == 2)
day = " terça-feira, "

else if(myday == 3)
day = " quarta-feira, "

else if(myday == 4)
day = " quinta-feira, "

else if(myday == 5)
day = " sexta-feira, "

else if(myday == 6)
day = " sábado, "

if(mymonth == 0)
month = " de janeiro de "

else if(mymonth ==1)
month = " de fevereiro de "

else if(mymonth ==2)
month = " de março de "

else if(mymonth ==3)
month = " de abril de "

else if(mymonth ==4)
month = " de maio de "

else if(mymonth ==5)
month = " de junho de "

else if(mymonth ==6)
month = " de julho de "

else if(mymonth ==7)
month = " de agosto de "

else if(mymonth ==8)
month = " de setembro de "

else if(mymonth ==9)
month = " de outubro de "

else if(mymonth ==10)
month = " de novembro de "

else if(mymonth ==11)
month = " de dezembro de "

document.write( day + myweekday + month + year);

How can I link it as a . js file and "call" the result of script within a DIV, for example?

1 answer

2


Create a . js file, for example data.js;

Inside the file, create a function that returns what you want;

She must look something like this:

function dataExtenso() {
/// Todo o seu processamento
//Agora basta trocar seu document.write( day + myweekday + month + year); por isso
return day + myweekday + month + year;
}

Import the file into the header of the html page where you want to use this result

<script src='caminho/para/seu/script/data.js'></script>

In your div, just call the function. Ai depends on how you want it. Supposing you did as I suggested, it would look more or less like this: ...

</div>
...
</body>
<script>
  document.getElementById("dataExtenso").innerHTML = dataExtenso();
</script>

This is like Neston, there are 1000 ways to do it. :)

Browser other questions tagged

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