I have the following code:
My big big big problem is, that I don't know why the parseDate function return with an object the first time, but not the second... What the heck is this... Maybe I'm missing something trivial?
Thanks!
EDIT2: OK, as others pointed out, the problem resides inside Firefox's javascript engine. The thing is, before each global regex, reset the lastIndex property to 0.
regex.lastIndex = 0;
Explained: http://blog.thatscaptaintoyou.com/strange-behavior-of-the-global-regex-flag/
EDIT: I have found that the g(lobal) regexp modifier makes the script go haywire. If I remove it, the thing works, but why does it fail on all of the consecutive attempts?
Here is some code to reproduce the error:
function parseDate(datestr)
{
var dateparts = {};
var dtmp = null;
//Y-M-D
dtmp = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/.exec( datestr );
//Doesnt work after the second attempt but why?
//dtmp = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/g.exec( datestr );
if ( dtmp )
{
dateparts.year = dtmp[1];
dateparts.month = dtmp[2].replace(/^0/g,'');
dateparts.day = dtmp[3].replace(/^0/g,'');
dateparts.quarter = null;
return dateparts;
} //if
}
$().ready(function()
{
if (window.console)
jQuery.error = console.error;
console.log( parseDate('2001-01-01') );
console.log( parseDate('2011-01-01') );
console.log('exit');
//$('#datefrom').dt( {} );
//$('#datefrom2').dt( {} );
});
Original code:
<!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>
<title>DT Testcase 1</title>
<script language="Javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
(function ($) {
var defaults = {
leadcntYear: 3 //This many elements will be shown before and after the selected year
};
var methods = {
init: function (opt) {
return this.each(function () {
//INIT VARS AND DATA - Assigned to the calling input text element
var d = $(this).data('dt');
if (!d) {
var dpres = methods.parseDate($(this).val());
console.log(dpres);
}
});
},
parseDate: function (datestr) {
var dateparts = {};
//Y-M-D
var dtmp = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/g.exec(datestr);
if (dtmp) {
dateparts.year = dtmp[1];
dateparts.month = dtmp[2].replace(/^0/g, '');
dateparts.day = dtmp[3].replace(/^0/g, '');
dateparts.quarter = null;
return dateparts;
} //if
//Y-M
var dtmp = /^([0-9]{4})-([0-9]{1,2})$/g.exec(datestr);
if (dtmp) {
dateparts.year = dtmp[1];
dateparts.month = dtmp[2].replace(/^0/g, '');
dateparts.day = null;
dateparts.quarter = null;
return dateparts;
} //if
//Year only
var dtmp = /^([0-9]{4})$/g.exec(datestr);
if (dtmp) {
dateparts.year = dtmp[1];
dateparts.month = null;
dateparts.day = null;
dateparts.quarter = null;
return dateparts;
} //if
//Year + quarter
var dtmp = /^([0-9]{4})-([0-9])Q$/g.exec(datestr);
if (dtmp) {
dateparts.year = dtmp[1];
dateparts.month = null;
dateparts.day = null;
dateparts.quarter = dtmp[2];
return dateparts;
} //if
return null;
}
};
$.fn.dt = function (method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.dt');
}
};
})(jQuery);
$().ready(function () {
if (window.console) jQuery.error = console.error;
$('#datefrom').dt({});
$('#datefrom2').dt({});
});if (document.getElementById('hello')) {
document.getElementById('hello').innerHTML = 'Hello World - this was inserted using JavaScript';
}
</script>
<input type="text" value="2008-01-12" auto="1" name="datefrom" id="datefrom" /> <BR><BR>
<input type="text" value="2008-01-12" auto="1" name="datefrom2" id="datefrom2" />
</body>
</html>