 |

04-28-2006, 12:36 PM
|
|
WebProWorld New Member
|
|
Join Date: Jan 2004
Location: Calgary
Posts: 17
|
|
Help needed to reference external javascript
I want to make a .js file containing javascript for the date on my website. I can add the file to the root dirtectory but I do not know how to reference the .js file so it appears on the page where I want it.
Any help would be appriciated.
|

04-28-2006, 02:45 PM
|
 |
WebProWorld Veteran
|
|
Join Date: Sep 2005
Location: Kerala, India
Posts: 397
|
|
Add the following line in the head of the document:
<script language="javascript" type="text/javascript" src="date.js"></script>
This would point to the .js file presuming it is named date.js.
Then add the following wherever you wish to see the date on the document:
<script language="JavaScript" type="text/javascript">print_date()</script>
This presuming the function called is named print_date(). That is all!
|

04-28-2006, 03:30 PM
|
|
WebProWorld New Member
|
|
Join Date: Jan 2004
Location: Calgary
Posts: 17
|
|
I am still doing something wrong. I named the script date.js (see script below)
<SCRIPT language=JavaScript>
<!--
// Insert current date
now = new Date();
day = now.getDay();
var dayname;
if (day == 0) dayname = "Sunday";
if (day == 1) dayname = "Monday";
if (day == 2) dayname = "Tuesday";
if (day == 3) dayname = "Wednesday";
if (day == 4) dayname = "Thursday";
if (day == 5) dayname = "Friday";
if (day == 6) dayname = "Saturday";
month = now.getMonth();
var monthname;
if (month == 0) monthname = "January";
if (month == 1) monthname = "February";
if (month == 2) monthname = "March";
if (month == 3) monthname = "April";
if (month == 4) monthname = "May";
if (month == 5) monthname = "June";
if (month == 6) monthname = "July";
if (month == 7) monthname = "August";
if (month == 8) monthname = "September";
if (month == 9) monthname = "October";
if (month == 10) monthname = "November";
if (month == 11) monthname = "December";
date = now.getDate();
year = now.getYear();
if (year < 1000) year += 1900;
document.write("<FONT SIZE=2 face=arial,helvetica,univers,sanserif color=black>" + dayname + ", " + monthname + " " + date + ", " + year + "</FONT>");
// -->
</SCRIPT>
But I am still getting errors. I am unsure of the referencing part of it.
|

04-28-2006, 09:56 PM
|
 |
WebProWorld Veteran
|
|
Join Date: Sep 2005
Location: Kerala, India
Posts: 397
|
|
Put this code into the .js file named date.js:
==================
<!--
// Insert current date
now = new Date();
day = now.getDay();
var dayname;
if (day == 0) dayname = "Sunday";
if (day == 1) dayname = "Monday";
if (day == 2) dayname = "Tuesday";
if (day == 3) dayname = "Wednesday";
if (day == 4) dayname = "Thursday";
if (day == 5) dayname = "Friday";
if (day == 6) dayname = "Saturday";
month = now.getMonth();
var monthname;
if (month == 0) monthname = "January";
if (month == 1) monthname = "February";
if (month == 2) monthname = "March";
if (month == 3) monthname = "April";
if (month == 4) monthname = "May";
if (month == 5) monthname = "June";
if (month == 6) monthname = "July";
if (month == 7) monthname = "August";
if (month == 8) monthname = "September";
if (month == 9) monthname = "October";
if (month == 10) monthname = "November";
if (month == 11) monthname = "December";
date = now.getDate();
year = now.getYear();
if (year < 1000) year += 1900;
theDate = "<FONT SIZE=2 face=arial,helvetica,univers,sanserif color=black>" + dayname + ", " + monthname + " " + date + ", " + year + "</FONT>";
// -->
==================
Then put this line in the head of the HTML document:
==================
<script language="javascript" type="text/javascript" src="date.js"></script>
==================
Then add this line wherever you wish to see the date in the HTML document:
==================
<script language="JavaScript" type="text/javascript">document.write(theDate)</script>
==================
|

04-28-2006, 09:57 PM
|
|
WebProWorld Veteran
|
|
Join Date: Aug 2003
Location: California,USA
Posts: 373
|
|
Just use the script itself without heading and ending. So remove the head (<SCRIPT language=JavaScript>) and ending (</SCRIPT>) from your js file. The name of the file tells what kind of file it is just like you wouldn't add <style type="text/css"> to a css file.
<!--
// Insert current date
now = new Date();
day = now.getDay();
var dayname;
if (day == 0) dayname = "Sunday";
if (day == 1) dayname = "Monday";
if (day == 2) dayname = "Tuesday";
if (day == 3) dayname = "Wednesday";
if (day == 4) dayname = "Thursday";
if (day == 5) dayname = "Friday";
if (day == 6) dayname = "Saturday";
month = now.getMonth();
var monthname;
if (month == 0) monthname = "January";
if (month == 1) monthname = "February";
if (month == 2) monthname = "March";
if (month == 3) monthname = "April";
if (month == 4) monthname = "May";
if (month == 5) monthname = "June";
if (month == 6) monthname = "July";
if (month == 7) monthname = "August";
if (month == 8) monthname = "September";
if (month == 9) monthname = "October";
if (month == 10) monthname = "November";
if (month == 11) monthname = "December";
date = now.getDate();
year = now.getYear();
if (year < 1000) year += 1900;
document.write("<FONT SIZE=2 face=arial,helvetica,univers,sanserif color=black>" + dayname + ", " + monthname + " " + date + ", " + year + "</FONT>");
// -->
|

05-15-2006, 10:08 AM
|
 |
WebProWorld Veteran
|
|
Join Date: Feb 2005
Location: Forchheim, Germany
Posts: 947
|
|
Quote:
|
Originally Posted by sands
Add the following line in the head of the document:
<script language="javascript" type="text/javascript" src="date.js"></script>
This would point to the .js file presuming it is named date.js.
|
If the script resides in the root directory, I suggest to use
Code:
<script language="javascript" type="text/javascript" src="/date.js"></script>
instead. This will work from all subdirectories as well.
hth,
faglork
|

05-16-2006, 12:43 AM
|
|
WebProWorld Pro
|
|
Join Date: May 2004
Location: Flagstaff, AZ
Posts: 178
|
|
Hi,
Another way to add JavaScript to a page is with Server Side Includes. Go here http://www.creativecauldron.com/websitearticleSSI.shtml and read my article "Use Server Side Includes To Ease Your Workload."
Have Fun,
Jeff
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|