I have a function with a stream of if statements. If a statement is true, it writes back to the document using innerHTML. Within the innerHTML I can add p tags and text, but as soon as I add an <a href> line it give me an "Uncaught SyntaxError: Unexpected identifier" error. What am I doing wrong? The error is within the calage() function with the following snippet:
if((calyear == 2010 && calmon > 10) || (calyear == 2011 && calmon < 11)) {
console.log("They should be in the 2YO");
document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> QSI Shekou!";
}
The full HTML file is below. Thank you for your help!
<!DOCTYPE HTML>
<html>
<head> </head>
<body>
<table cellpadding=0 cellspacing=0 style="width:95%;" align="center">
<tr><td align="center" class="abouttabletext"><br>
<script type="text/javascript">
var startyear = "1995";
var endyear = "2013";
var dat = new Date();
var curday = dat.getDate();
var curmon = dat.getMonth()+1;
var curyear = dat.getFullYear();
function checkleapyear(datea)
{
if(datea.getYear()%4 == 0)
{
if(datea.getYear()% 10 != 0)
{
return true;
}
else
{
if(datea.getYear()% 400 == 0)
return true;
else
return false;
}
}
return false;
}
function DaysInMonth(Y, M) {
with (new Date(Y, M, 1, 12)) {
setDate(0);
return getDate();
}
}
function datediff(date1, date2) {
var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(),
y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate();
if (d1 < d2) {
m1--;
d1 += DaysInMonth(y2, m2);
}
if (m1 < m2) {
y1--;
m1 += 12;
}
return [y1 - y2, m1 - m2, d1 - d2];
}
function calage()
{
var calday = document.birthday.day.options[document.birthday.day.selectedIndex].value;
var calmon = document.birthday.month.options[document.birthday.month.selectedIndex].value;
var calyear = document.birthday.year.options[document.birthday.year.selectedIndex].value;
if(curday =="" || curmon=="" || curyear=="" || calday=="" || calmon=="" || calyear=="")
{
alert("please fill all the values and click go -");
}
else
{
var curd = new Date(curyear,curmon-1,curday);
var cald = new Date(calyear,calmon-1,calday);
var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0);
var dife = datediff(curd,cald);
var monleft = (dife[0]*12)+dife[1];
var secleft = diff/1000/60;
var hrsleft = secleft/60;
var daysleft = hrsleft/24;
var as = parseInt(calyear)+dife[0]+1;
var datee = diff/1000/60/60/24;
}
if((calyear == 2010 && calmon > 10) || (calyear == 2011 && calmon < 11)) {
console.log("They should be in the 2YO");
document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> Visit W3Schools!";
}
if((calyear == 2009 && calmon > 10) || (calyear == 2010 && calmon < 11)) {
console.log("They should be in the 3YO");
return 3;
}
if((calyear == 2008 && calmon > 10) || (calyear == 2009 && calmon < 11)) {
console.log("They should be in the 4YO");
}
if((calyear == 2007 && calmon > 10) || (calyear == 2008 && calmon < 11)) {
console.log("They should be in the 5YO");
}
if((calyear == 2006 && calmon > 10) || (calyear == 2007 && calmon < 11)) {
console.log("They should be in the 6YO");
}
if((calyear == 2005 && calmon > 10) || (calyear == 2006 && calmon < 11)) {
console.log("They should be in the 7YO");
}
if((calyear == 2004 && calmon > 10) || (calyear == 2005 && calmon < 11)) {
console.log("They should be in the 8YO");
}
if((calyear == 2003 && calmon > 10) || (calyear == 2004 && calmon < 11)) {
console.log("They should be in the 9YO");
}
if((calyear == 2002 && calmon > 10) || (calyear == 2003 && calmon < 11)) {
console.log("They should be in the 10YO");
}
if((calyear == 2001 && calmon > 10) || (calyear == 2002 && calmon < 11)) {
console.log("They should be in the 11YO");
}
if((calyear == 2000 && calmon > 10) || (calyear == 2001 && calmon < 11)) {
console.log("They should be in the 12YO");
}
if((calyear == 1999 && calmon > 10) || (calyear == 2000 && calmon < 11)) {
console.log("They should be in the 13YO");
}
if((calyear == 1998 && calmon > 10) || (calyear == 1999 && calmon < 11)) {
console.log("They should be in the 14YO");
}
if((calyear == 1997 && calmon > 10) || (calyear == 1998 && calmon < 11)) {
console.log("They should be in the 15YO");
}
if((calyear == 1996 && calmon > 10) || (calyear == 1997 && calmon < 11)) {
console.log("They should be in the 16YO");
}
if((calyear == 1995 && calmon > 10) || (calyear == 1996 && calmon < 11)) {
console.log("They should be in the 17YO");
}
if(calyear == 1995 && calmon < 11) {
console.log("Your child is too old to attend QSI");
}
}
</script>
<form name="birthday" action="">
Date<select name="day" size="1">
<script type="text/javascript">
for(var j=1;j<32;j++)
document.write("<option value="+j+">"+j+"</option>");
</script></select>
Month<select name="month" size="1">
<script type="text/javascript">
for(var i=1;i<13;i++)
document.write("<option value="+i+">"+i+"</option>");
</script></select>
Year
<select name="year" size="1">
<script type="text/javascript">
for(var k=startyear;k<endyear;k++)
document.write("<option value="+k+">"+k+"</option>");
</script></select>
<br><br>
<input name="start" onclick="calage()" value="Calculate" type="button">
</form>
</td></tr></table>
<div id="demo">
</div>
</body>
</html>
try this:
document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> <a href='http://qsishekou.org' target='_blank'>QSI Shekou!</a>";
enclose the string with "", like you have - so use ' to surround attributes.
for example:
var str = "hello, I am" gonna go" here";
that will generate an error because you close the string and then you don't concatenate, thus the exception.
so,
var str = "hello, I am' gonna go' here";
would be appropriate.
or
var str = "hello, I am" + "gonna go here";
or if you have to use double quotes, then backslash them.
var str = "hello, I am\" gonna go\" here";
In my case, I already used simple quotes as #jamesemanon suggests, but it still didn't work. In Android devices no problem, IOS simulator no problem, but IOS devices failed.
The solution for me was just remove the target="_blank" attr, and link work now on devices.
Related
I have written the code below. I am asking for a user prompt for the values of day
and month and I want to return their star sign based on the values that they submit.
I do not know how to get the result of their input displayed on a web page.
So
user prompt for day
user prompt for month
then I want the starsign returned based on the else/if code. Can anybody help?
Thanks
var StarSign=
["Aries","Taurus","Gemini","Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius"
"Capricorn","Aquarius","Pisces"]
function signOftheZodiac(theDay,theMonth){
var theDay=prompt("What is your day of birth");
var theMonth=prompt("What is your month of birth");
if ((theDay>=21 && theMonth=="March") || (theDay<=19 && theMonth=="April")){
return StarSign[0];
}else if ((theDay>=20 && theMonth=="April")||(theDay<=20 && theMonth=="May")){
return StarSign[1];
}else if ((theDay>=21 && theMonth=="May")||(theDay<=20 && theMonth=="June")){
return StarSign[2];
}else if((theDay>=21 && theMonth=="June") ||(theDay<=22 && theMonth=="July")){
return StarSign[3]
}else if((theDay>=23 && theMonth=="July")||(theDay<=22 && theMonth=="August")){
return StarSign[4]
}else if((theDay>=23 && theMonth=="August")||(theDay<=22 && theMonth=="September")){
return StarSign[5]
}else if((theDay>=23 && theMonth=="September")||(theDay<=22 && theMonth=="October")){
return StarSign[6]
}else if((theDay>=23 && theMonth=="October")||(theDay<=21 && theMonth=="November")){
return StarSign[7]
}else if((theDay>=22 && theMonth=="November")||(theDay<=21 && theMonth=="December")){
return StarSign[8]
}else if((theDay>=22 && theMonth=="December")||(theDay<=19 && theMonth=="January")){
return StarSign[9]
}else if((theDay>=20 && theMonth=="January")||(theDay<=18 && theMonth=="February")){
return StarSign[10]
}else if((theDay>=19 && theMonth=="February")||(theDay<=20 && theMonth=="March")){
result= StarSign[11]
}
var h1=document.createElement("h1")
var textAnswer=document.createTextNode("Your Star sign is")
h1.setAttribute("id","signOftheZodiac");
h1.appendChild(textAnswer);
document.getElementById("flex-child").appendChild(h1)
}
function reset(){
document.getElementById("signOftheZodiac").remove();
}
I wouldn't want to do this with JS-Dialogs (which are ugly). Rather use HTML-Input that you can style (and optionally hide once you calculated the sign).
Something like this: (note that I fixed minor errors in your JS where you assigned result instead of returning it. Hope it's useable for your purposes ;)
var StarSign = ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius",
"Capricorn", "Aquarius", "Pisces"
];
function signOftheZodiac() {
var theDay = document.getElementById("day").value;
var d=document.getElementById("month");
var theMonth = d.options[d.selectedIndex].text;
;
console.log(theDay,theMonth);
if ((theDay >= 21 && theMonth == "March") || (theDay <= 19 && theMonth == "April")) {
return StarSign[0];
} else if ((theDay >= 20 && theMonth == "April") || (theDay <= 20 && theMonth == "May")) {
return StarSign[1];
} else if ((theDay >= 21 && theMonth == "May") || (theDay <= 20 && theMonth == "June")) {
return StarSign[2];
} else if ((theDay >= 21 && theMonth == "June") || (theDay <= 22 && theMonth == "July")) {
return StarSign[3]
} else if ((theDay >= 23 && theMonth == "July") || (theDay <= 22 && theMonth == "August")) {
return StarSign[4]
} else if ((theDay >= 23 && theMonth == "August") || (theDay <= 22 && theMonth == "September")) {
return StarSign[5]
} else if ((theDay >= 23 && theMonth == "September") || (theDay <= 22 && theMonth == "October")) {
return StarSign[6]
} else if ((theDay >= 23 && theMonth == "October") || (theDay <= 21 && theMonth == "November")) {
return StarSign[7]
} else if ((theDay >= 22 && theMonth == "November") || (theDay <= 21 && theMonth == "December")) {
return StarSign[8]
} else if ((theDay >= 22 && theMonth == "December") || (theDay <= 19 && theMonth == "January")) {
return StarSign[9]
} else if ((theDay >= 20 && theMonth == "January") || (theDay <= 18 && theMonth == "February")) {
return StarSign[10]
} else if ((theDay >= 19 && theMonth == "February") || (theDay <= 20 && theMonth == "March")) {
return StarSign[11]
}
}
function calc() {
var h1 = document.createElement("h1");
var textAnswer = document.createTextNode("Your Star sign is " + signOftheZodiac());
console.info(textAnswer);
h1.setAttribute("id", "signOftheZodiac");
h1.appendChild(textAnswer);
document.getElementById("flex-child").appendChild(h1)
}
function reset() {
document.getElementById("signOftheZodiac").remove();
}
<select id="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input type="number" id="day">
<button type="button" onclick="calc()">Calc</button>
<p id="flex-child"></p>
Hi I need those two to show in the same line(Now they show one after another). I tried many things but nothing works.
document.write();
var day = new Date();
var hr = day.getHours();
if ((hr == 1) || (hr == 2) || (hr == 3) || (hr == 4) || (hr == 5) || (hr ==
6) || (hr == 7) || (hr == 8) || (hr == 9) || (hr == 10) || (hr == 11) ||
(hr == 12)) {
document.write("Good Morning!");
}
if ((hr == 13) || (hr == 14) || (hr == 15) || (hr == 16) || (hr == 17)) {
document.write("Good Afternoon!");
}
if ((hr == 18) || (hr == 19) || (hr == 20) || (hr == 21) || (hr == 22) ||
(hr == 23) || (hr == 24)) {
document.write("Good Evening!");
}
document.write();
/*--------------------------------------------------------------------------
---------------------------*/
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
person;
}
}
Is this what you are looking for:
<html>
<body onload="myFunction()">
<div id="demo"></div>
<script>
function myFunction() {
var day = new Date();
var hr = day.getHours();
var greeting;
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
if ( hr <= 12 ) {
greeting = "Good morning " + person;
} else if ( hr <= 18 ) {
greeting = "Good afternoon " + person;
} else {
greeting = "Good evening " + person;
}
document.getElementById("demo").innerHTML = greeting;
}
}
</script>
</body>
</html>
I hope this'll Help you. i also done code optimization for if
Conditions.
You have to invoke myfunction method
<body>
<span id="demo"></span>
<script>
(function () {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.write();
var day = new Date();
var hr = day.getHours();
if ((hr >= 1) && (hr <= 12)) {
person = person + " Good Morning!";
}
else if ((hr >= 13) && (hr <= 17)) {
person = person + " Good Afternoon!";
}
else if ((hr >= 18) && (hr <= 24)) {
person = person + "Good Evening!" ;
}
document.getElementById("demo").innerHTML = person;
}
})()
</script>
</body>
document.write() does not add a new line to document but rather changes your document element to the content, where as document.getElementById() refers to specific element in the DOM. Anyway here is my
javascript:
function greeting() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
// Set hr to current hour
var hr = new Date().getHours();
// Tests current hour
if (hr > 0 && hr < 13) {
document.write("Good Morning! " + person);
} else if (hr > 12 && hr < 18) {
document.write("Good Afternoon! " + person);
} else if (hr > 17 && hr < 25) {
document.write("Good Evening! " + person);
}
}
}
I'm trying to show a link only at certain times of the day.
The link is visible within Adobe Muse, but is hidden on browser.
Here's my code
<script type="text/javascript">
var day= new Date();
var hr= day.getUTCHours();
if ((hr == 0) || (hr == 1) || (hr == 2) || (hr == 3) || (hr == 4) || (hr == 5) || (hr == 6) || (hr == 7) || (hr == 8) || (hr == 9) || (hr == 10) || (hr == 11) || (hr == 12) || (hr == 13) || (hr == 22) || (hr == 23)) { var a = document.createElement('a'); var linkText = document.createTextNode("Example"); a.appendChild(linkText); a.title = "Example"; a.style.fontSize = "16px"; a.style.color="#C31E2F"; a.href = "http://www.example.com/"; document.body.appendChild(a);}
if ((hr == 14) || (hr == 15) || (hr == 16) || (hr == 17) || (hr == 18) || (hr == 19) || (hr == 20) || (hr == 21)) { document.write("<br><font>CALL TOLL FREE:xxx-xxx-xxxx</font></br>");}
What do you mean it's hidden? This script is getting time from your device local time. If it's correct, you should get correct result.
<script type="text/javascript">
var day= new Date();
var hr= day.getUTCHours();
if ((hr>=0 && hr<13) || (hr>=22 && hr<=23)) {
var a = document.createElement('a');
var linkText = document.createTextNode("Example");
a.appendChild(linkText); a.title = "Example";
a.style = "font-size: 16px; #C31E2F; color: #C31E2F";
a.href = "http://www.example.com/";
document.body.appendChild(a);
} else {
var out=document.createElement("P");
out.innerHTML = "<br>CALL TOLL FREE:xxx-xxx-xxxx</br>";
document.body.appendChild(out);
}
</script>
Ended up trashing most of the existing code. I think document.createElement was causing the problem.
Here's what I'm using instead.
<script type="text/javascript">
var day= new Date();
var hr= day.getUTCHours();
if ((hr>=0 && hr<13) || (hr>=22 && hr<=23)) {
document.write('<font>Example</font>'.link('http://www.example.com/')); }
else { document.write("<font>CALL TOLL FREE:xxx-xxx-xxxx</font>"); }
So I'm creating a rescission calendar, and need to make some changes to the javascript, not really sure how.
All the code I create has to be inserted into a CMS system, which strips the head/body tag, so I can't use the onLoad function. What I want to have happen, is once they select a date, they click a submit button, and then the rescission date appears.
Here's the code from our previous system which didn't use a CMS:
Startscript language="javascript" type="text/javascript"<br>
hoyDate = new Date;<br>
closeDate = new Date;<br>
fundDate = new Date;<br>
resDate = new Date;<br>
monthsi = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');<br>
daysi = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');<br>
function setForm() {<br>
document.form.monthy.options.selectedIndex = closeDate.getMonth();<br>
document.form.datey.options.selectedIndex = closeDate.getDate() - 1;<br>
document.form.yeary.value = closeDate.getFullYear();<br>
}<br><br>
function calcRescission() {<br>
closeDate.setMonth(document.form.monthy.options[document.form.monthy.options.selectedIndex].value * 1);<br>
closeDate.setDate(document.form.datey.options[document.form.datey.options.selectedIndex].value * 1);<br>
closeDate.setFullYear(document.form.yeary.value * 1);<br>
<br>
resDate.setTime(closeDate.getTime());<br>
fundDate.setTime(closeDate.getTime());<br>
<br>
for (i=0; i<=2; i++) {<br>
resDate.setTime(resDate.getTime() + (3600000 * 24));<br>
//check for New Year's Day<br>
if (resDate.getMonth() == 0 && resDate.getDate() == 1) {<br>
i--;<br>
}<br>
//check for Christmas<br>
if (resDate.getMonth() == 11 && resDate.getDate() == 25) {<br>
i--;<br>
}<br>
//check for the 4th of July<br>
if (resDate.getMonth() == 6 && resDate.getDate() == 4) {<br>
i--;<br>
}<br>
//check for Veterans Day<br>
if (resDate.getMonth() == 10 && resDate.getDate() == 11) {<br>
i--;<br>
}<br>
//check for Memorial Day<br>
if (resDate.getMonth() == 4 && resDate.getDate() >= 25 && resDate.getDate() <= 31 && resDate.getDay() == 1) {<br>
i--;<br>
}<br>
//check for Labor Day<br>
if (resDate.getMonth() == 8 && resDate.getDate() >= 1 && resDate.getDate() <= 7 && resDate.getDay() == 1) {<br>
i--;<br>
}<br>
//check for Thanksgiving<br>
if (resDate.getMonth() == 10 && resDate.getDate() >= 22 && resDate.getDate() <= 28 && resDate.getDay() == 4) {<br>
i--;<br>
}<br>
//check for Martin Luther King, Jr observance<br>
if (resDate.getMonth() == 0 && resDate.getDay() == 1 && resDate.getDate() >= 15 && resDate.getDate() <= 21) {<br>
i--;<br>
}<br>
//check for Washington/Lincoln observance<br>
if (resDate.getMonth() == 1 && resDate.getDay() == 1 && resDate.getDate() >= 15 && resDate.getDate() <= 21) {<br>
i--;<br>
}<br>
//check for Columbus Day<br>
if (resDate.getMonth() == 9 && resDate.getDay() == 1 && resDate.getDate() >= 8 && resDate.getDate() <= 14) {<br>
i--;<br>
}<br>
//check for Sundays<br>
if (resDate.getDay() == 0) {<br>
i--;<br>
}<br>
}<br>
j = 0;<br>
fundit = "no";<br>
while (j<=3 || fundit != "yes") {<br>
fundit = "yes";<br>
j++;<br>
fundDate.setTime(fundDate.getTime() + (3600000 * 24));<br>
//checking for Christmas<br>
if ((fundDate.getMonth() == 11 && fundDate.getDate() == 25) || (fundDate.getMonth() == 11 && fundDate.getDate() == 26 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getMonth() == 11 && fundDate.getDate() == 25 && fundDate.getDay() == 0) {<br>
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//checking for New Year's<br>
if ((fundDate.getMonth() == 0 && fundDate.getDate() == 1) || (fundDate.getMonth() == 0 && fundDate.getDate() == 2 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getDate() == 1 && fundDate.getMonth() == 0 && fundDate.getDay() == 0) {
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//checking for the 4th of July<br>
if ((fundDate.getMonth() == 6 && fundDate.getDate() == 4) || (fundDate.getMonth() == 6 && fundDate.getDate() == 5 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getDate() == 4 && fundDate.getMonth() == 6 && fundDate.getDay() == 0) {
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//check for Memorial Day<br>
if (fundDate.getMonth() == 4 && fundDate.getDay() == 1 && fundDate.getDate() >= 25 && fundDate.getDate() <= 31) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//check for Labor day<br>
if (fundDate.getMonth() == 8 && fundDate.getDay() == 1 && fundDate.getDate() >= 1 && fundDate.getDate() <= 7) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//check for Thanksgiving<br>
if (fundDate.getMonth() == 10 && fundDate.getDay() == 4 && fundDate.getDate() >= 22 && fundDate.getDate() <= 28) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//check for Veterans Day<br>
if ((fundDate.getMonth() == 10 && fundDate.getDate() == 11) || (fundDate.getMonth() == 10 && fundDate.getDate() == 12 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getDate() == 11 && fundDate.getMonth() == 10 && fundDate.getDay() == 0) {<br>
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//checking for Martin Luther King, Jr observance<br>
if (fundDate.getMonth() == 0 && fundDate.getDay() == 1 && fundDate.getDate() >= 15 && fundDate.getDate() <= 21) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//checking for Washington/Lincoln's Day<br>
if (fundDate.getMonth() == 1 && fundDate.getDay() == 1 && fundDate.getDate() >= 15 && fundDate.getDate() <= 22) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//checking for Columbus Day<br>
if (fundDate.getMonth() == 9 && fundDate.getDay() == 1 && fundDate.getDate() >= 8 && fundDate.getDate() <= 14) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//checking for weekends<br>
if (fundDate.getDay() == 6 || fundDate.getDay() == 0) {<br>
fundit = "no";<br>
if (fundDate.getDay() == 0) {<br>
j--;<br>
}<br>
}<br>
}<br>
//feed rescission end date to form<br>
// document.form.rescissionday.value = daysi[resDate.getDay()];<br>
document.form.rescissiondatey.value = resDate.getDate();<br>
document.form.rescissionmonthy.value = monthsi[resDate.getMonth()];<br>
document.form.rescissionyeary.value = resDate.getFullYear();<br>
//feed funding date to form<br>
document.form.fundday.value = daysi[fundDate.getDay()];<br>
document.form.funddatey.value = fundDate.getDate();<br>
document.form.fundmonthy.value = monthsi[fundDate.getMonth()];<br>
document.form.fundyeary.value = fundDate.getFullYear();<br>
document.form.nowday.value = daysi[closeDate.getDay()];<br>
}<br>
Endscript<br>
Here's the HTML:
<body onload="setForm();calcRescission();">
<form name="form" action="rescission.html" method="post">
<table cellpadding="4" cellspacing="0">
<tr>
<td>
<select name="monthy" onchange="calcRescission()" class="editable">
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
</select>
<select name="datey" onchange="calcRescission()" class="editable">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input name="yeary" onkeyup="calcRescission()" size="7" class="editable">
</td>
</tr>
<tr>
<td>
<input name="rescissionmonthy" readonly size="10" class="rescind">
<input name="rescissiondatey" readonly size="3" class="rescind">
<input name="rescissionyeary" readonly size="7" class="rescind">
</td>
</tr>
</table>
</form>
You'll get rid of this:
<body onload="setForm();calcRescission();">
and add this to the script:
document.getElementsByTagName("body")[0].addEventListener("load", function() {
setForm();calcRescission()
}, false);
How do you make something with JavaScript that when your time is below 12 o'clock, it says "good morning!" and when it is after 12 o'clock, it says "good afternoon!"?
Here it is!
var d = new Date();
var time = d.getHours();
if (time < 12) {
document.write("<b>Good morning!</b>");
}
if (time > 12) {
document.write("<b>Good afternoon!</b>");
}
if (time == 12) {
document.write("<b>Go eat lunch!</b>");
}
const date = new Date;
let hours = date.getHours();
let status = (hours < 12)? "Morning" :
((hours <= 18 && hours >= 12 ) ? "Afternoon" : "Night");
console.log(status);
The following should work:
var hours = new Date().hours;
if(hours > 12){
alert("Good Afternoon!");
}
else{
alert("Good Morning!");
}
Just for fun, here's a one liner:
(new Date().hours > 12) ? alert("Good Afternoon!") : alert("Good Morning!");
Working Demo
<SCRIPT LANGUAGE="JavaScript">
currentTime=new Date();
//getHour() function will retrieve the hour from current time
if(currentTime.getHours()<12)
document.write("<b>Good Morning!! </b>");
else if(currentTime.getHours()<17)
document.write("<b>Good Afternoon!! </b>");
else
document.write("<b>Good Evening!! </b>");
</SCRIPT>
//if hour is between 6am-12pm ,print good morning
//if hour is between 12pm-6pm ,print good afternoon
//else good evening
let hour = 5;
if(hour>=6 && hour<12) {
console.log('Good Morning')
}
else if(hour>+12 && hour<18) {
console.log('Good Afternoon')
}
else {
console.log('Good Evening')
}
<script type="text/javascript">
document.write("<h2>");
var day = new Date();
var hr = day.getHours();
if (hr >= 4 && hr < 12) {
document.write("Goedenmorgen");
} else if (hr == 12) {
document.write("Goedemiddag");
} else if (hr >= 12 && hr <= 16) {
document.write("Goedemiddag");
} else if (hr >= 16 && hr <= 23) {
document.write("Goedenavond");
} else {
document.write("Goedennacht");
}
document.write("</h2>");
</script>