Calling JavaScript

T

Trevor L.

I have a script which generates the date. (I could attach
it but it is straightforward for those who know
JavaScript)

How do I call this from the HTML code inside the <body>
tag,
e.g.
<p>Welcome. Todays date is XXXXXXXXXXXX </p>

What to do I put in place of XXXXXXXXXXXX to display the
date?

At the moment I am putting the entire script. This is a
bit of overkill.

The code looks like this
<html>

<head>
</head>

<body>

<p>Welcome. Todays date is&nbsp;
<script language="JavaScript">
<!--
(the script goes in here)
//-->
</script>
</p>

</body>
</html>

The result is
Welcome. Todays date is Friday, January 21, 2005
 
S

Steve Easton

Not as easy to say as you think without seeing the script.
If the script is using document.all.tagid.innerHTML= "what to write";
Where tagid = the id assigned to the <p tag like this" id=tagid"
and where what to write = the actual portion of the script that generates the date.

The you could launch the script by adding this to the body tag: <body onload="functionname()">
Where functionname() = the function that starts the script.

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 
B

Bob Lehmann

If the script is using document.all.tagid.innerHTML= "what to write";
This would only work in IE.

document.getElementById(tagid).innerHTML

Bob Lehmann
 
B

Bob Lehmann

Meant to say...

This would be better -
document.getElementById(tagid).innerHTML

Bob Lehmann
 
S

Steve Easton

Thanks Bob.
I tend to forget about the "other" browsers.

;-)

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 
M

Murray

<p>Today's date is <script
type="text/javsacript">document.write(yourfunctionname())</script>, and
you'd better believe it!</p>
 
T

Trevor L.

Hi Steve, Bob, Murray

All the script does is calculate the date into the variable today. The last
statement is
document.write(today)

Here it is in full. (I don't understand it all as yet, but that doesn't
matter.)

<script language="JavaScript">
<!--
var now = new Date();
var days = new Array(
'Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
var months = new Array(
'January','February','March','April','May',
'June','July','August','September','October',
'November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;}

today = days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear()));

document.write(today);
//-->
</script>


Murray,
I used your suggestion

I named the function TodaysDate(), changed the last line to return (today)
and placed it in the <head> section

In the body I did exactly as you said, i.e.
<p>Today's date is
<script type="text/javascript">document.write(TodaysDate())</script>
, and you'd better believe it!</p>

It returned:
Today's date is Saturday, January 22, 2005 , and you'd better believe it!
as hoped for

Steve and Bob,
I don't understand your suggestions as yet.
The script is not using document.all.tagid.innerHTML

With a bit of experimentation, it may become clear
--
Thanks to all,
Trevor L.


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 
S

Steve Easton

To get the script to do what you want as it is written, place it in a single cell table, or a cell
within a table like this.

<table border="0" cellpadding="0" cellspacing="0" width="200" height="20">
<tr>
<td align="center">
<script language="JavaScript">
<!--
var now = new Date();
var days = new Array(
'Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
var months = new Array(
'January','February','March','April','May',
'June','July','August','September','October',
'November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;}

today = days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear()));

document.write(today);
//-->
</script>
</td>
</tr>
</table>


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top