paulmo Posted February 2, 2009 Share Posted February 2, 2009 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 More sharing options...
jeffman Posted February 2, 2009 Share Posted February 2, 2009 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 More sharing options...
paulmo Posted February 2, 2009 Author Share Posted February 2, 2009 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 More sharing options...
jeffman Posted February 2, 2009 Share Posted February 2, 2009 The first two should be all you need, assuming initDate() is stored in time_greeting.js . The third script's a mess, and also not necessary. (innerHTML is a property, not a function.) Link to comment Share on other sites More sharing options...
paulmo Posted February 3, 2009 Author Share Posted February 3, 2009 that's not doing anything. where/how to call/echo the actual greeting in html, like in a <p> tag? Link to comment Share on other sites More sharing options...
jeffman Posted February 3, 2009 Share Posted February 3, 2009 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 More sharing options...
paulmo Posted February 3, 2009 Author Share Posted February 3, 2009 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 More sharing options...
jlhaslip Posted February 3, 2009 Share Posted February 3, 2009 try this: <h1 id="dtField">good evening, <?php echo $post_name; ?></h1> Link to comment Share on other sites More sharing options...
paulmo Posted February 3, 2009 Author Share Posted February 3, 2009 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 More sharing options...
jeffman Posted February 3, 2009 Share Posted February 3, 2009 Okay, I'm confused. You want this in javascript or PHP? You never said anything about PHP before, and this is a Javascript thread. It's not like they work together. Link to comment Share on other sites More sharing options...
paulmo Posted February 3, 2009 Author Share Posted February 3, 2009 didn't mean to be confusing. topic seems to apply to javascript insofar as whether or not javascript can appear on same line with other script. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 3, 2009 Share Posted February 3, 2009 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 More sharing options...
paulmo Posted February 4, 2009 Author Share Posted February 4, 2009 thanks...is this close? <script type="text/javascript">var username = "<?php echo $name; ?>";<h1 id="dtField">document.write(username());</h1></script> Link to comment Share on other sites More sharing options...
justsomeguy Posted February 5, 2009 Share Posted February 5, 2009 Close, but username is not a function, it's just a variable. You don't put parentheses after the identifier when refering to a variable. Link to comment Share on other sites More sharing options...
paulmo Posted February 5, 2009 Author Share Posted February 5, 2009 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 More sharing options...
Synook Posted February 5, 2009 Share Posted February 5, 2009 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 More sharing options...
paulmo Posted February 5, 2009 Author Share Posted February 5, 2009 i'm just experimenting. i'm trying to get js greeting (which works) followed by $post_name so it will be like: Good afternoon, Synook.is this going to be possible? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.