As I update the Web pages over at JavaScript Source, I am also trying to update some of the scripts. I know that some of the scripters here like a challenge so, being that I myself am still learning, here is what I am proposing:
Using this thread, I would post a script from JSS that seems like it could be written more efficiency. Then one (or more) of the forum members could suggest a more efficient, workable way to write it and submit it here. I would add your name to the credits with a link to your Web site. I'm not talking about real complicated ones.
By doing this, it helps to raise the quality of the scripts and gives you credit and a link in return. In addition. it also would help other forum members to learn different ways of coding.
Do I have any takers? Suggestions, ideas, and comment are welcome.
One of them is mine, and you're more than welcome to "update" it. It's a modified script of Ronnie's and since it's now 6+ years old it could prolly do with some revision.
Great! It looks like this could be fruitful. I appreciate the input.
For starters, I have a pretty simple one (at least I think it's simple). This pulldown menu will automatically adjust the range of months so that the current month is at the top. The remaining months are placed in order after the current month. It seems like the script could be written better than using document.writeln. Maybe placing it in a DIV, perhaps. Well, anyway, have a go. Remember, just be sure to give me the name and link you want credit listed for (I'll try to keep track so you won't have to do it in the future). Also, feel free to add/change certain portions of these scripts. If they become completely different, I'll just post them as new instead of updated.
Here is the script:
Code:
function autoMonth() {
var time = new Date();
var month = time.getMonth();
var date = month - 12;
var realJavaScriptMonth = month;
var future = month + 0;
month = month + 1; /* Compensate for "January" being "0" */
document.writeln ("<form><select><option value=\"\">Select Month");
do {
month = date;
if (month >= 12) {
month = month - 12;
}
if (month < 0) {
month = month + 12;
}
date++;
var dateName ="";
switch (month) {
case 0:
dateName = "January";
break;
case 1:
dateName = "February";
break;
case 2:
dateName = "March";
break;
case 3:
dateName = "April";
break;
case 4:
dateName = "May";
break;
case 5:
dateName = "June";
break;
case 6:
dateName = "July";
break;
case 7:
dateName = "August";
break;
case 8:
dateName = "September";
break;
case 9:
dateName = "October";
break;
case 10:
dateName = "November";
break;
case 11:
dateName = "December";
break
}
month++;
realJavaScriptMonth++;
document.write ("<option value=\"" + realJavaScriptMonth + "\">" + dateName + "");
realJavaScriptMonth++;
}
while (date < future)
document.write ("</select></form>");
}
Here's an implementation using the JavaScript DOM:
Code:
function autoMonth() {
var months = new Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
var curMonth = (new Date()).getMonth();
// Create the form element.
var form = document.createElement("form");
document.body.appendChild(form);
// Create the dropdown.
var select = document.createElement("select");
form.appendChild(select);
// Add the "Select Month" default value.
select.options[0] = new Option("Select Month", "");
// Using j as an index to the select.options length,
// add months, starting with the current month.
var j = 1;
for (var i=curMonth; i<12; i++) {
select.options[j] = new Option(months[i], i);
j++;
}
// Add the remaining months, starting with January.
for (var i=0; i<curMonth; i++) {
select.options[j] = new Option(months[i], i);
j++;
}
}
You're right - you generally shouldn't use document.write (or document.writeln) - since you're just writing out raw HTML. The preferred method is to use the DOM, using createElement() calls as per above.
Note also that you can just use a months names array, and access it via the index.
Thanks guys, nice work. Just a few questions/observations:
@Bear: How would you implement it on the page? What you have locates it at the bottom. How could it be implemented within a form?
@Chugster: This looks great. Nice and compact. Let me ask you also, how would you implement it on the page, without using the script tag to call the function? How could it be implemented within a form?
It just takes what is there in the HTML and rearranges it so that the current month is first followed by the rest.
The other codes are good examples of js creating html. Mine is an example of rearranging existing HTML. So if you already had the HTML code, you could just add the script (after the form tag, or in an onload handler) to reorder the months.
Bookmarks