ca_redwards
11-25-2002, 04:31 PM
I have created a pair of bookmarklets to implement a 100% pure JavaScript
pop-up calendar. The first bookmarklet defines a library of HTML building
functions for utilization by the second in building and displaying a pop-up
calendar. This calendar has a hidden field to hold its date value, a
drop-down list to select a different month, a textbox to enter a different
year, and an array of buttons to select a different day of month. It is
this button array that seems to have an intermittent problem with becoming mysteriously undefined.
Initially, the calendar’s buttons are all blank and an opener window
function labels them with the appropriate days of the month. This
post-rendering technique works as designed. However, when I attempt to change the month, a JavaScript error implies that the button array is undefined. After dismissing the error dialog, the calendar renders okay for the new month.
What is going on?
Here are my two bookmarklets:
<a href="javascript:
function Q(V){return' Q'+(V?('='+String.fromCharCode(34)+V+String.fromCharCode(34)):'')};
function X(V){return'<X'+(V?V:'')+'>'};
function Z(V){return'<Z'+(V?V:'')+'>'+this+'</'+'Z>'};
function G(V){return'<G'+(V?V:'')+'>'+this.join('<G'+(V?V:'')+'>')};
function R(V){return'<R'+(V?V:'')+'>'+this.join('</'+'R><R'+(V?V:'')+'>')+'</'+'R>'};
String.prototype.gen=function gen()
{var js='';var arr=this.split(' ');
for(var i=0;i<arr.length;i++)
{var t=arr[i];var T=t.toUpperCase();
if(t==t.toLowerCase()){js+='window.'+T+'='+Q.toString().replace(/Q/g,T)+';'}
else{if(t==T){js+='String.prototype.'+T+'='+Z.toString().replace(/Z/g,T)+';';
js+='Array.prototype.'+T+'='+R.toString().replace(/R/g,T)+';'}
else{js+='window.'+T+'='+X.toString().replace(/X/g,T)+';';
js+='Array.prototype.'+T+'='+G.toString().replace(/G/g,T)+';'}};};
eval(js);return this;};
dhtml='A action align alt B background Base bgcolor BIG
BLINK BODY border bordercolor bordercolordark bordercolorlight Br cellpadding cellspacing checked color cols
colspan compact content dir DIV enctype face FONT FORM H1
H2 H3 H4 H5 H6 HEAD height Hr href hspace HTML I id Img
Input lang language leftmargin LI marginheight marginwidth
maxlength Meta method name NOSCRIPT noshade nowrap
OL onblur onchange onclick onfocus onload onmouseout
onmouseover onreset onselect onsubmit onunload OPTION P
PRE profile readonly rows rowspan SCRIPT SELECT size SMALL
SPAN src start STRIKE STYLE style SUB SUP TABLE target TD
TEXTAREA TH TITLE title topmargin TR TT type U UL valign
value vspace width wrap'.gen();
window.status='HTML() bookmarklet library: Copyright (c) 2002, by Richard Edwards.';
void(0);// dhtml: Copyright (c) 2002, by Richard Edwards.">HTML()</a>
<a href="javascript:
m='January February March April May June July August September October November December'.split(' ');
wd='Su M Tu W Th Fr Sa'.split(' ');
lom=[31,28,31,30,31,30,31,31,30,31,30,31];
sz=25;
function getdate(F)
{if(F.date.value){DATE=F.date.value.split('/');M=DATE[0]-1;D=DATE[1];Y=DATE[2]}
else{DATE=new Date();M=DATE.getMonth();D=DATE.getDate();Y=DATE.getFullYear()};
lom[1]=28;
if((Y% 4==0)&&((Y% 100>0)||(Y% 400==0))){lom[1]++};};
function setdate(F){F.date.value=(1.0+M)+'/'+D+'/'+Y;};
function render(F){alert(F.date.value)};
function newday(){getdate(this.form);D=this.value;setdate(this.form);alert(this.form.date.value)};
function newyear(){getdate(this.form);Y=this.value;setdate(this.form);render(this.form)};
function newmonth(){getdate(this.form);M=this.selectedIndex;setdate(this.form);render(this.form)};
window.render=function render(F)
{if(!F)F=this.form;
getdate(F); f=new Date(Y,M,1);x=1-(f.getDay());
for(i=0;i<49;i++){F.d[i].value=(i<7?wd[i]:'');
F.d[i].onclick=this};
for(i=1;i<=lom[M];i++){F.d[7+i-x].value=i;F.d[7+i-x].onclick=newday};
for(i=0;i<m.length;i++){F.m[i].selected=(i==M);};
F.m.onchange=newmonth; F.y.value=Y;F.y.onchange=newyear;
F.d[7+D-x].focus();
setdate(F);};
r=new Array(7).INPUT(TYPE('button')+NAME('d')+STYLE('width:'+sz+';height:'+sz)).TD(ALIGN('right')+WIDTH(sz *7)+HEIGHT(sz));
h=m.OPTION().SELECT(NAME('m'))+INPUT(TYPE('text')+SIZE(4)+NAME('y'))+INPUT(TYPE('hidden')+NAME('date '));
h=(h.TD(COLSPAN(7)+ALIGN('center')).TR()+[r,r,r,r,r,r,r].TR()).TABLE(BORDER('0')+CELLSPACING('0')+CELLPADDING('0')).FORM(NAME('frm'));
h+='opener.render(document.forms.frm);'.SCRIPT(TYPE('text/javascript'));
window.open('','asdf','height='+sz*9+',width='+sz*8).document.write(h);
void(0);// calendar: Copyright (c) 2002, by Richard Edwards.">calendar</a>
pop-up calendar. The first bookmarklet defines a library of HTML building
functions for utilization by the second in building and displaying a pop-up
calendar. This calendar has a hidden field to hold its date value, a
drop-down list to select a different month, a textbox to enter a different
year, and an array of buttons to select a different day of month. It is
this button array that seems to have an intermittent problem with becoming mysteriously undefined.
Initially, the calendar’s buttons are all blank and an opener window
function labels them with the appropriate days of the month. This
post-rendering technique works as designed. However, when I attempt to change the month, a JavaScript error implies that the button array is undefined. After dismissing the error dialog, the calendar renders okay for the new month.
What is going on?
Here are my two bookmarklets:
<a href="javascript:
function Q(V){return' Q'+(V?('='+String.fromCharCode(34)+V+String.fromCharCode(34)):'')};
function X(V){return'<X'+(V?V:'')+'>'};
function Z(V){return'<Z'+(V?V:'')+'>'+this+'</'+'Z>'};
function G(V){return'<G'+(V?V:'')+'>'+this.join('<G'+(V?V:'')+'>')};
function R(V){return'<R'+(V?V:'')+'>'+this.join('</'+'R><R'+(V?V:'')+'>')+'</'+'R>'};
String.prototype.gen=function gen()
{var js='';var arr=this.split(' ');
for(var i=0;i<arr.length;i++)
{var t=arr[i];var T=t.toUpperCase();
if(t==t.toLowerCase()){js+='window.'+T+'='+Q.toString().replace(/Q/g,T)+';'}
else{if(t==T){js+='String.prototype.'+T+'='+Z.toString().replace(/Z/g,T)+';';
js+='Array.prototype.'+T+'='+R.toString().replace(/R/g,T)+';'}
else{js+='window.'+T+'='+X.toString().replace(/X/g,T)+';';
js+='Array.prototype.'+T+'='+G.toString().replace(/G/g,T)+';'}};};
eval(js);return this;};
dhtml='A action align alt B background Base bgcolor BIG
BLINK BODY border bordercolor bordercolordark bordercolorlight Br cellpadding cellspacing checked color cols
colspan compact content dir DIV enctype face FONT FORM H1
H2 H3 H4 H5 H6 HEAD height Hr href hspace HTML I id Img
Input lang language leftmargin LI marginheight marginwidth
maxlength Meta method name NOSCRIPT noshade nowrap
OL onblur onchange onclick onfocus onload onmouseout
onmouseover onreset onselect onsubmit onunload OPTION P
PRE profile readonly rows rowspan SCRIPT SELECT size SMALL
SPAN src start STRIKE STYLE style SUB SUP TABLE target TD
TEXTAREA TH TITLE title topmargin TR TT type U UL valign
value vspace width wrap'.gen();
window.status='HTML() bookmarklet library: Copyright (c) 2002, by Richard Edwards.';
void(0);// dhtml: Copyright (c) 2002, by Richard Edwards.">HTML()</a>
<a href="javascript:
m='January February March April May June July August September October November December'.split(' ');
wd='Su M Tu W Th Fr Sa'.split(' ');
lom=[31,28,31,30,31,30,31,31,30,31,30,31];
sz=25;
function getdate(F)
{if(F.date.value){DATE=F.date.value.split('/');M=DATE[0]-1;D=DATE[1];Y=DATE[2]}
else{DATE=new Date();M=DATE.getMonth();D=DATE.getDate();Y=DATE.getFullYear()};
lom[1]=28;
if((Y% 4==0)&&((Y% 100>0)||(Y% 400==0))){lom[1]++};};
function setdate(F){F.date.value=(1.0+M)+'/'+D+'/'+Y;};
function render(F){alert(F.date.value)};
function newday(){getdate(this.form);D=this.value;setdate(this.form);alert(this.form.date.value)};
function newyear(){getdate(this.form);Y=this.value;setdate(this.form);render(this.form)};
function newmonth(){getdate(this.form);M=this.selectedIndex;setdate(this.form);render(this.form)};
window.render=function render(F)
{if(!F)F=this.form;
getdate(F); f=new Date(Y,M,1);x=1-(f.getDay());
for(i=0;i<49;i++){F.d[i].value=(i<7?wd[i]:'');
F.d[i].onclick=this};
for(i=1;i<=lom[M];i++){F.d[7+i-x].value=i;F.d[7+i-x].onclick=newday};
for(i=0;i<m.length;i++){F.m[i].selected=(i==M);};
F.m.onchange=newmonth; F.y.value=Y;F.y.onchange=newyear;
F.d[7+D-x].focus();
setdate(F);};
r=new Array(7).INPUT(TYPE('button')+NAME('d')+STYLE('width:'+sz+';height:'+sz)).TD(ALIGN('right')+WIDTH(sz *7)+HEIGHT(sz));
h=m.OPTION().SELECT(NAME('m'))+INPUT(TYPE('text')+SIZE(4)+NAME('y'))+INPUT(TYPE('hidden')+NAME('date '));
h=(h.TD(COLSPAN(7)+ALIGN('center')).TR()+[r,r,r,r,r,r,r].TR()).TABLE(BORDER('0')+CELLSPACING('0')+CELLPADDING('0')).FORM(NAME('frm'));
h+='opener.render(document.forms.frm);'.SCRIPT(TYPE('text/javascript'));
window.open('','asdf','height='+sz*9+',width='+sz*8).document.write(h);
void(0);// calendar: Copyright (c) 2002, by Richard Edwards.">calendar</a>