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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.