Link to home
Start Free TrialLog in
Avatar of dmalovich
dmalovich

asked on

This javascript code works in mozilla but it freezes in IE

I've attached the code.  It works fine in mozilla and chrome but it freezes when run in IE 7 or 8.
/* Set days in month script
   Copyright (c) 2003-2010 Michel Plungjan "javascripts(a)plungjan.name" */
// Change the values below if needed:

var yearName ="txtYear";
var monthName="txtMonth";
var dayName  ="txtDay";
var formIndex = 0; /* 0 for the first form on the page, name can be used but is not valid in some validators */

var adjustYears=true; // fix the year drop down to be around now.
var yearAdjustment=-100; // how many years prior to this year
var numberOfYears = 101; // how many years in the dropdown

function setDate(theForm) {
  var ySel = document.forms[formIndex].elements[yearName];
  var mSel = document.forms[formIndex].elements[monthName];
  var dSel = document.forms[formIndex].elements[dayName];

  var year = ySel.options[ySel.selectedIndex].value
  if (!year) return
  var month = mSel.options[mSel.selectedIndex].value
  if (!month) return
  var d = new Date(year,month,0); // last day in the previous month because months start on 0
  var daysInMonth = d.getDate();
  dSel.options.length=29;// save the minimum days
  if (daysInMonth>28) for (i=29;i<=daysInMonth;i++) {
    dSel.options.length++
    dSel.options[dSel.options.length-1] = new Option(i,i)
  }
  setSelectedDate(theForm);
}
function setSelectedDate(theForm) {
  var ySel = document.forms[formIndex].elements[yearName];
  var year = ySel.options[ySel.selectedIndex].value
  var mSel = document.forms[formIndex].elements[monthName];
  var month = mSel.options[mSel.selectedIndex].value
  var dSel = document.forms[formIndex].elements[dayName];
  var day  = dSel.options[dSel.selectedIndex].value

  var mm = parseInt(month);
  if (mm<10) mm="0"+mm;
  var dd = (day<10)?"0"+day:day;
  theForm.selectedDate.value=year.substring(2)+''+mm+''+dd;
}
function setNow() {
  var now = new Date()
  var ySel = document.forms[formIndex].elements[yearName];
  var mSel = document.forms[formIndex].elements[monthName];
  var dSel = document.forms[formIndex].elements[dayName];

  var year = now.getFullYear();

  if (adjustYears) {
    var firstYear = year + yearAdjustment;
    ySel.options.length=1; // remove all but 1st option
    for (var i=firstYear, n=firstYear+numberOfYears;i<n;i++) {
      ySel.options.length++
      ySel.options[ySel.options.length-1] = new Option(i,i)
    }
  }
  var minYear = parseInt(ySel.options[1].value);
  var diff = year-minYear+1;
  ySel.selectedIndex=((diff)>0)?diff:0;

  mSel.selectedIndex = now.getMonth()+1;
  dSel.selectedIndex = now.getDate(); // NB: Not 0 based
  setSelectedDate(document.forms[formIndex]);
 
}
var data = "";
window.onload=function() {
  setNow();
}
var saveDate = "";
function findIt(theForm) {
  if (saveDate) document.getElementsByName('a'+saveDate)[0].style.backgroundColor='white'
  var date = theForm.selectedDate.value;
  var anchor = document.getElementsByName('a'+date);
  if (anchor.length==0) {
    alert('The Market was closed on this day');
    return;
  } 
  anchor[0].style.backgroundColor='red'
  anchor[0].scrollIntoView();
  saveDate=date;
}

Open in new window

Avatar of pradyahuja
pradyahuja
Flag of Australia image

works fine for me
<script>
/* Set days in month script
   Copyright (c) 2003-2010 Michel Plungjan "javascripts(a)plungjan.name" */
// Change the values below if needed:

var yearName ="txtYear";
var monthName="txtMonth";
var dayName  ="txtDay";
var formIndex = 0; /* 0 for the first form on the page, name can be used but is not valid in some validators */

var adjustYears=true; // fix the year drop down to be around now.
var yearAdjustment=-100; // how many years prior to this year
var numberOfYears = 101; // how many years in the dropdown

function setDate(theForm) {
  var ySel = document.forms[formIndex].elements[yearName];
  var mSel = document.forms[formIndex].elements[monthName];
  var dSel = document.forms[formIndex].elements[dayName];

  var year = ySel.options[ySel.selectedIndex].value
  if (!year) return
  var month = mSel.options[mSel.selectedIndex].value
  if (!month) return
  var d = new Date(year,month,0); // last day in the previous month because months start on 0
  var daysInMonth = d.getDate();
  dSel.options.length=29;// save the minimum days
  if (daysInMonth>28) for (i=29;i<=daysInMonth;i++) {
    dSel.options.length++
    dSel.options[dSel.options.length-1] = new Option(i,i)
  }
  setSelectedDate(theForm);
}
function setSelectedDate(theForm) {
  var ySel = document.forms[formIndex].elements[yearName];
  var year = ySel.options[ySel.selectedIndex].value
  var mSel = document.forms[formIndex].elements[monthName];
  var month = mSel.options[mSel.selectedIndex].value
  var dSel = document.forms[formIndex].elements[dayName];
  var day  = dSel.options[dSel.selectedIndex].value

  var mm = parseInt(month);
  if (mm<10) mm="0"+mm;
  var dd = (day<10)?"0"+day:day;
  alert(mm)
  theForm.selectedDate.value=year.substring(2)+''+mm+''+dd;
}
function setNow() {
  var now = new Date()
  var ySel = document.forms[formIndex].elements[yearName];
  var mSel = document.forms[formIndex].elements[monthName];
  var dSel = document.forms[formIndex].elements[dayName];

  var year = now.getFullYear();

  if (adjustYears) {
    var firstYear = year + yearAdjustment;
    ySel.options.length=1; // remove all but 1st option
    for (var i=firstYear, n=firstYear+numberOfYears;i<n;i++) {
      ySel.options.length++
      ySel.options[ySel.options.length-1] = new Option(i,i)
    }
  }
  var minYear = parseInt(ySel.options[1].value);
  var diff = year-minYear+1;
  ySel.selectedIndex=((diff)>0)?diff:0;

  mSel.selectedIndex = now.getMonth()+1;
  dSel.selectedIndex = now.getDate(); // NB: Not 0 based
  setSelectedDate(document.forms[formIndex]);
 
}
var data = "";
window.onload=function() {
  setNow();
}
var saveDate = "";
function findIt(theForm) {
  if (saveDate) document.getElementsByName('a'+saveDate)[0].style.backgroundColor='white'
  var date = theForm.selectedDate.value;
  var anchor = document.getElementsByName('a'+date);
  if (anchor.length==0) {
    alert('The Market was closed on this day');
    return;
  } 
  anchor[0].style.backgroundColor='red'
  anchor[0].scrollIntoView();
  saveDate=date;
}
</script>

var yearName ="txtYear";
var monthName="txtMonth";
var dayName  ="txtDay";
var formIndex = 0; /* 0 for the first form on the page, name can be used but is not valid in some validators */

<form>
<select id="txtYear">
</select>
<select id="txtMonth">
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>

</select>
<select id="txtDay">

<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
</select>
<input type="text" id="selectedDate">
</form>

Open in new window

Avatar of dmalovich
dmalovich

ASKER

what did you change in the script?
almost nothing..
I just added my html for testing
is it working for u as well?
if yes, then have a look at how u r rendering out select drop down
it works in ie when developing on my localhost. When its on the internet others who use ie have problems with it freezing
Avatar of Michel Plungjan
There is nothing special about the script
The experts will not be able to figure this out from the script alone
It freezes when you have 10000 lines of data in the page
It does work fine in firefox though.  So anything less than 10000 lines and it will work in explorer?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial