This is my first Question posted to Stack Overflow, so please forgive me if i'm approaching this incorrectly.
What I am trying to accomplish: creating a dynamic pop up that displays one of two messages depending on a set range of dates.
An example of the parameters for this message would be:
The fee from September 1 of an odd year to April 30 of an even year is $149
The fee from May 1 of an even year to August 31 of an odd year is $249
Dependent on these date ranges, I would like the pop-up to display only the current fee
Currently, I have a static pop-up that I created, but am unsure as to how I would re-factor this to dialog display the aforementioned content dynamically. I realize this is an extremely simple script in its current state, but any help would be greatly appreciated. I've searched for help, but have been unable to find anything on this specific type of pop-up
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TEST</title>
<script>
function MyFunction()
{
alert("The fee from September 1st of an odd year to April 30th of an even year is $149 \nThe fee from May 1st of an even year to August 31st of an odd year is $249");
}
</script>
</head>
<a id="myLink" title="Click to display fee" href="Service_Pack_93_Planned_VR.pdf" onclick="MyFunction()">Application Form</a>
<body>
</body>
</html>
EDIT After digging deeper into conditional statements and date functions in JavaScript, I came up with the code below. This was before I saw aholmes's response (which is incredibly helpful). I plan to refactor what I currently have so that my code is more effective and accommodates what I have learned from your responses.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function myFunction()
{
var x="";
var startDate1=new Date('09/01/2013');
var endDate1=new Date('04/30/2014');
var startDate2=new Date('05/01/2014');
var endDate2=new Date('08/31/2015');
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'/'+dd+'/'+yyyy;
if (startDate1 < today < endDate1)
{
x="The fee is $" + "149"
}
else if (startDate2 < today < endDate2)
{
x="The fee is $" + "249"
}
document.getElementById("test").innerHTML=x;
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
<p id="test"></p>
</body>
</html>
EDIT
I had been trying to get my code working since updating my variables using the information aholmes provided me with, but had been unsuccessful. Today I came into work and modified some of the variables and I believe it is working. Thank you again for ALL of your help. If there is anything you see that could be cleaned up or modified, please let me know
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script>
function myFunction()
{
var x="";
// get the current date
var date = new Date(); // Fri Feb 07 2014 11:34:32 GMT-0800 (PST)
// this will return an integer, counting months starting from 0 (January). Since it is February at the moment, this will return 1.
var month = date.getMonth(); // 1
// this returns the full year as an integer - so 2014.
var year = date.getFullYear(); // 2014
var yearIsOdd = (year % 2 === 1); // false
// remember, months are 0-based, so September (the 9th month) is represented by an 8.
if (month >= 8 && yearIsOdd) {} // false - only month is checked due to short-circuiting.
console.log("month:" + month + " Year:" + year + " Odd?" + yearIsOdd)
// Remember order of operations. Division (and thus, modulo) happens before addition without parentheses!
var nextYearIsEven = !((year + 1) % 2); // false
if ((month >= 8 && yearIsOdd) || (month <= 3 && !yearIsOdd))
{x="The fee is $" + "149"} // false - again, only month is checked
else
{x="The fee is $" + "249"}
document.getElementById("test").innerHTML=x;
}
</script>
<body>
<button onclick="myFunction()">Try it</button>
<p id="test"></p>
</body>
</html>
Thank you in advance!