Jump to content

Date Greeting Help Please


paulmo

Recommended Posts

would like this code to work. if someone could please tell me how, and possibly how to use it as external file and "call" it in html, (tried "id=dtField" with no success), i'd be grateful.

<script language="text/javascript>function initDate() {	var now = new Date();	document.getElementById("dtField").innerHTML = timeString(now.getHours());function timeString(theHour) {	if (theHour < 5) {		return "You're up early.";	}	if (theHour < 9) {		return "Get up.";	}	if (theHour < 17) {		return "Good day.";	}	return "Good evening.";	}}</script>

Link to comment
Share on other sites

Just a few modifications. You missed a closing brace, and the if else construction is preferable for this sort of task.

function initDate() {	var now = new Date();	document.getElementById("dtField").innerHTML = timeString(now.getHours());}function timeString(theHour) {	if (theHour < 5) {		return "You're up early.";	} else if (theHour < 9) {		return "Get up.";	} else if (theHour < 17) {		return "Good day.";	} else {		return "Good evening.";	}}

You can put those two functions in an external file (name it date.js or something alse suitable). External js files have NO <script> tags. Just the code. The tags get embedded in the head of your main document. Somewhere below these tags (probably in a window.onload handler), simply call initDate(). As long as there is an element called "dtField" in your doc, it will work.

Link to comment
Share on other sites

thanks for getting me going. so i've got

window.onload=initDate;

as first line of external (function) script. in doc head:

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

and trying to call in body:

<script language="text/javascript">innerHTML(initDate("dtField"));</script>

how close?

Link to comment
Share on other sites

Ohhhh!When you wrote document.getElementById("dtField"), I just assumed you already had an element in the page with that id.Yeah, either of these could work:<p id="dtField"><div id="dtField">And that should be the only addition you need.

Link to comment
Share on other sites

thanks, you got me set, almost! i need to echo php on same line after id=dtField, like "good evening, jim." i've tried <h1> and <p> tags, but of course php code begins below the js. how to get all on same line? thanks<h1 id="dtField"></h1><?php echo $post_name; ?>

Link to comment
Share on other sites

thanks for the suggestion; it doesn't work. the greeting (good evening, etc.) is in external js file, called by dtField. that's working. the $post_name is wrapping to another line and i need it on same line after greeting.if i can't do this in dtField statement with html tags, solution to $post_name to external js file? or??thanks

Link to comment
Share on other sites

You can output PHP anywhere. If you're doing something with Javascript, you can use PHP to write the value of the name to a Javascript variable and then use it in JS from there.

<script type="text/javascript">var username = "<?php echo $name; ?>";</script>

Link to comment
Share on other sites

ok i took off the () and it's still not working.

<script type="text/javascript">var username = "<?php echo $name; ?>";<h1 id="dtField">document.write(username);</h1></script>

thanks for bearing with me...i'm tired and mistook variable for function here (and in php post...)

Link to comment
Share on other sites

You can't put HTML within JS like that, you need to print it. Anyway, why are you using JS redundantly like that?

<script type="text/javascript">var username = "<?php echo $name; ?>";document.write('<h1 id="dtField">');document.write(username);document.write('</h1>');</script>

That is equvalent to:

<h1 id="dtField"><?php echo $name; ?></h1>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...