How to execute external JavaScript linked

1

I have the JavaScript of 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 the script within a DIV, for example?

    
asked by anonymous 05.05.2015 / 14:21

1 answer

3

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

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

It should look 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. It depends on how you want it. Assuming you did as I suggested, it would look something like this:          ...     

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

That's the same as Neston, there are 1000 ways to do it. :)

    
05.05.2015 / 15:07