Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do i insert this code into a html from a external .js file?

window.onload = function () {
    setTimeout("time_date(), 1000");
}

function time_date() {
    ourDate = new Date();
    document.getElementById("cap1").innerHTML = "La fecha y la hora en la ubicación de su equipo es: " + ourDate.toLocaleString();

    document.getElementById("cap2").innerHTML = "El desfase entre la hora local y la zona horaria es GMT " + ourDate.getTimezoneOffset() + " minutes ";

    document.getElementById("cap3").innerHTML = "La hora y la fecha (GMT) son: " + ourDate.toGMTString() + ".<br/>");
}
share|improve this question
 
Please provide the HTML code. –  Thibault 14 hours ago
add comment

2 Answers

Put this code between <HEAD> tags.

<script type="text/javascript" src="timedate.js"></script>
share|improve this answer
 
I already have that,but i want to insert that code into a <div> lets say.i want to control where the time will show. –  user3111007 14 hours ago
 
Javascript code should not be inserted into a div. Let it in a Javascript file, and make the code write where it should. For example, your code actually write into #cap1, #cap2 and #cap3 divs. –  Thibault 14 hours ago
 
It should work like that.It has to write into the cap1,2,3 but it doesn't. –  user3111007 14 hours ago
 
I think there is a mistake in the use of setTimeout. –  Paul 14 hours ago
 
Try this : setTimeout("time_date()", 1000); –  Thibault 13 hours ago
add comment

Insert a script tag just before the closing </body> if you don't need your javascript to run before the page is loaded. The page will be loaded before the JavaScript and displayed faster than if you included the script in the head.

<script type="text/javascript" src="your_script.js"></script>
</body>

More information on where to put js files can be found there : JavaScript at bottom/top of web page?

And I think you have a problem without your timeout, it should read :

setTimeout(time_date, 1000);

And a typo : there is an unexpected ) in your last line.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.