Jump to content

Issue External JS Files: Trying to place a current date on my site.


Barons

Recommended Posts

Hi,

 

I don't know anything about JS so I found some copy and paste code online telling me how to put a current date on my website. It works like a charm. the only issue is I have tons of code in the middle of my html page. I started trying to find out how to include it like you would a php file.

 

I've got the part about creating a .js file without the script tags and I've got the part about adding stuff into the header of the page but them what?

 

Anyone able to help me figure out what I need to stick in the middle of my div tags to make the date show up? I used the below site to tell me how to do what I've done so far. Its about mid way down on the page.

http://www.netmechanic.com/news/vol5/html_no3.htm

 

Here's where i'm at if it helps.

 

Header:

<script src="date.js" language="javascript" type="text/javascript"></script>

 

date.js file:

<!-- 

// Array of day names
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
			"Thursday","Friday","Saturday");

// Array of month Names
var monthNames = new Array(
"January","February","March","April","May","June","July",
"August","September","October","November","December");

var now = new Date();
document.write(dayNames[now.getDay()] + ", " + 
monthNames[now.getMonth()] + " " + 
now.getDate() + ", " + now.getFullYear());

// -->

 

=( Lonely div tags:

<div id="datetext"></div>

 

Am I missing something major that will require me to lean how this stuff works? Hopefully its just a simple bit of code. If not I might just leave it the way it was.

 

Thanks for the help.

You would need to create an onload event and instead of document.write, use document.getElementById('datetext').innerHTML = '....'; in the onload function. If you don't want to make an onload event, just move all your javascript files to the bottom of the page (above the </body> tag).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.