Click to See Complete Forum and Search --> : Problem!
I have pasted some javascript into html which displays a text message, time and date. However I would like to align it somehow so it appears in the middle instead of the left hand side.
Would be very apprechiative if someone could help.
Many Thanks.
clairec666
12-04-2003, 06:31 AM
Have you used document.write to put the text onto the page?
Thank you for the reply.
I didnt actually program the script myself, I simply copied and pasted the script into the html.
I dont know what "document.write" is, and as far as I am aware - have not used it.
fredmv
12-04-2003, 11:43 AM
Could we see the source code for your script? It would make it easier for us to help you. All you should need to do is use CSS to center the text, for example, at the beginning of the string you could do this:<div style="text-align: center;">Then at the end of the string:</div>All you're really doing is wrapping a <div> styled with CSS around the string of text in order to center it. As for your question on document.write, it's a function in JavaScript in which prints text.
Thank you for your reply.
Using css: will this align text produced by javascript?
Here is the source code embedded within the html.
http://www.geocities.com/ibart2002/script_test.html
-----------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
<!--
// Store the date in a variable
d = new Date()
dateText = ""
// Get the current day and convert it to the name of the day
dayValue = d.getDay()
if (dayValue == 0)
dateText += "Sunday"
else if (dayValue == 1)
dateText += "Monday"
else if (dayValue == 2)
dateText += "Tuesday"
else if (dayValue == 3)
dateText += "Wednesday"
else if (dayValue == 4)
dateText += "Thursday"
else if (dayValue == 5)
dateText += "Friday"
else if (dayValue == 6)
dateText += "Saturday"
// Get the current month and convert it to the name of the month
monthValue = d.getMonth()
dateText += " "
if (monthValue == 0)
dateText += "January"
if (monthValue == 1)
dateText += "February"
if (monthValue == 2)
dateText += "March"
if (monthValue == 3)
dateText += "April"
if (monthValue == 4)
dateText += "May"
if (monthValue == 5)
dateText += "June"
if (monthValue == 6)
dateText += "July"
if (monthValue == 7)
dateText += "August"
if (monthValue == 8)
dateText += "September"
if (monthValue == 9)
dateText += "October"
if (monthValue == 10)
dateText += "November"
if (monthValue == 11)
dateText += "December"
// Get the current year; if it's before 2000, add 1900
if (d.getYear() < 2000)
dateText += " " + d.getDate() + ", " + (1900 + d.getYear())
else
dateText += " " + d.getDate() + ", " + (d.getYear())
// Get the current minutes
minuteValue = d.getMinutes()
if (minuteValue < 10)
minuteValue = "0" + minuteValue
// Get the current hours
hourValue = d.getHours()
// Customize the greeting based on the current hours
if (hourValue < 12)
{
greeting = "Good morning!"
timeText = " at " + hourValue + ":" + minuteValue + " AM"
}
else if (hourValue == 12)
{
greeting = "Good afternoon!"
timeText = " at " + hourValue + ":" + minuteValue + " PM"
}
else if (hourValue < 17)
{
greeting = "Good afternoon!"
timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM"
}
else
{
greeting = "Good evening!"
timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM"
}
// Write the greeting, the date, and the time to the page
document.write(greeting + " It's " + dateText + timeText)
//-->
</SCRIPT>
--------------------------------------------------------------------
fredmv
12-04-2003, 12:05 PM
This is by no means the best way to do it (and that code could be shortened quite a bit), but this will center it for you:<div style="text-align: center;">
<SCRIPT LANGUAGE="JavaScript">
<!--
// Store the date in a variable
d = new Date()
dateText = ""
// Get the current day and convert it to the name of the day
dayValue = d.getDay()
if (dayValue == 0)
dateText += "Sunday"
else if (dayValue == 1)
dateText += "Monday"
else if (dayValue == 2)
dateText += "Tuesday"
else if (dayValue == 3)
dateText += "Wednesday"
else if (dayValue == 4)
dateText += "Thursday"
else if (dayValue == 5)
dateText += "Friday"
else if (dayValue == 6)
dateText += "Saturday"
// Get the current month and convert it to the name of the month
monthValue = d.getMonth()
dateText += " "
if (monthValue == 0)
dateText += "January"
if (monthValue == 1)
dateText += "February"
if (monthValue == 2)
dateText += "March"
if (monthValue == 3)
dateText += "April"
if (monthValue == 4)
dateText += "May"
if (monthValue == 5)
dateText += "June"
if (monthValue == 6)
dateText += "July"
if (monthValue == 7)
dateText += "August"
if (monthValue == 8)
dateText += "September"
if (monthValue == 9)
dateText += "October"
if (monthValue == 10)
dateText += "November"
if (monthValue == 11)
dateText += "December"
// Get the current year; if it's before 2000, add 1900
if (d.getYear() < 2000)
dateText += " " + d.getDate() + ", " + (1900 + d.getYear())
else
dateText += " " + d.getDate() + ", " + (d.getYear())
// Get the current minutes
minuteValue = d.getMinutes()
if (minuteValue < 10)
minuteValue = "0" + minuteValue
// Get the current hours
hourValue = d.getHours()
// Customize the greeting based on the current hours
if (hourValue < 12)
{
greeting = "Good morning!"
timeText = " at " + hourValue + ":" + minuteValue + " AM"
}
else if (hourValue == 12)
{
greeting = "Good afternoon!"
timeText = " at " + hourValue + ":" + minuteValue + " PM"
}
else if (hourValue < 17)
{
greeting = "Good afternoon!"
timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM"
}
else
{
greeting = "Good evening!"
timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM"
}
// Write the greeting, the date, and the time to the page
document.write(greeting + " It's " + dateText + timeText)
//-->
</SCRIPT>
</div>
Works...
Thank you very much for your help.
I am not a javascript programmer, but I apprechiate your insight towards the script.
Thanks again.
fredmv
12-04-2003, 01:12 PM
You're very welcome. :D