Monday, July 23, 2012

How to really get the right version of Internet Explorer in JavaScript



This is a simple function to really return the right version of Internet Explorer. Pieces are cobbled together from my old work and dojo's inner libraries too.

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
//In cases where the page has an HTTP header or META tag with
//X-UA-Compatible, then it is in emulation mode.
//Make sure isIE reflects the desired version.
//document.documentMode of 5 means quirks mode.
//Only switch the value if documentMode's major version
//is different from isIE's major version.
var mode = document.documentMode;
if(mode && mode != 5 && Math.floor(rv) != mode){
rv = mode;
}
  }
  return rv;
}

Wednesday, July 18, 2012

TimeTextBox in Dojo


//I wrote this to overcome a limitation in dojo. I created a combined Date Time control
// and wrote this to enable the time control part to have a start and end time set on it


dojo.declare("com.kana.base.widget.field.TimeTextBox", dijit.form.TimeTextBox, {
rangeCheck: function(/*Date*/date, constraints) {
// summary:
// disables dates outside of allowedDates, or if there are no allowedDates,
//  outside of the min/max of the _DateTimeTextBox
if(this.selector == "time") {
date = dojo.date.locale.format(date, {
selector: "time",
timePattern: "HH:mm:ss"
});
date = date.split(":");
date = parseFloat(date[0] + ":" + date[1]);

if(constraints.max) {
var max = constraints.max.split(":");
max = parseFloat(max[0] + "." + max[1]);
}

if(constraints.min) {//if it is less than the max and greater than the min
var min = constraints.min.split(":");
min = parseFloat(min[0] + '.' + min[1]);
}
if(max && min) {
return (max >= date) && (date >= min);
}
else if(max)
return (max >= date);
else if(min)
return (date >= min);
}
return this.inherited(arguments);
},
setShowing: function(isShowing) {
if(this.showing != isShowing) {
this.showing = isShowing;
this.domNode.style.display = isShowing ? '' : 'none';
}
},
_end: 0
});

// Prototype to get the readonly string for date.
com.kana.base.widget.field.DateTime.getReadOnlyString = function(dateTimeString, datePattern, timePattern, showDate, showTime) {

if(dateTimeString) {
var constr = {};
var dateString = "";
var timeString = "";
if(!(showDate === false)) {
if(datePattern) {
constr.datePattern = datePattern;
}
else {
constr.datePattern = "M/d/yyyy";
//default
}
constr.selector = "date";
dateString = dojo.date.locale.format(new Date(parseInt(dateTimeString)), constr) + " ";
}
if(!(showTime === false)) {
if(timePattern) {
constr.timePattern = timePattern;
}
else {
constr.timePattern = "h:mm a";
//default
}
constr.selector = "time";
timeString = dojo.date.locale.format(new Date(parseInt(dateTimeString)), constr);
}

return dateString + timeString;
}
return "";
//if passed null, give blank string
};