I was having a problem when l am trying to load a page in through ajax. I have found what the problem is, and it is a script that I have that converts an ISO date into a readable one. If I remove the code block from the page, it will load the page fine. But if I add it back, then try to load that page through ajax, the browser will hang, and go to a white screen. The page being loaded in has some document.write code, could this be the problem?
Here is the code block, that when removed, lets the page load:
Code:
<script type="text/javascript">
//Needed to to remove time for calendar
Date.prototype.customFormat=function(formatString){
var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhh,hh,h,mm,m,ss,s,ampm,dMod,th;
YY = ((YYYY=this.getFullYear())+"").substr(2,2);
MM = (M=this.getMonth()+1)<10?('0'+M):M;
MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substr(0,3);
DD = (D=this.getDate())<10?('0'+D):D;
DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this.getDay()]).substr(0,3);
th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);
h=(hhh=this.getHours());
if (h==0) h=24;
if (h>12) h-=12;
hh = h<10?('0'+h):h;
ampm=hhh<12?'am':'pm';
mm=(m=this.getMinutes())<10?('0'+m):m;
ss=(s=this.getSeconds())<10?('0'+s):s;
return formatString.replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm);
}
getTFID=function(isoDate){ //GET TIME FROM ISO DATE
if(isoDate.slice(-1)=='-')
return '';
isoDate = isoDate.replace(/\D/g, " ");
var dObj = isoDate.split(" ");
var newDate = new Date(dObj[0], (dObj[1]-1), dObj[2], dObj[3], dObj[4], dObj[5]);
return newDate.customFormat('#h#:#mm##ampm#');
}
</script>
If you are loading that through a page and using innerHTML to put it in, than that is your problem. JavaScript is not executed in that manner.
Eric
The large block of code is NOT brought in through ajax, it is in an external .js file which is linked in the header. The only javascript that IS brought in through the ajax page, is the document write:
That document.write line will not run since as I said before Ajax does not run JavaScript.
Why are you using Ajax? To get the date? You really should just be returning the date, getting the value with responseText, run that value through your function, and display it on the page with innerHTML.
Why are you using Ajax? To get the date? You really should just be returning the date, getting the value with responseText, run that value through your function, and display it on the page with innerHTML.
Ok, let me explain a little more. My page has a left hand navigation, which allows you to load that page in through ajax, into a div on that page. One of these pages has a calendar, which I convert the ISO date using javascript. Since this calendar is included on of the the pages that is brought in through ajax, this causes the problem. Also, there are many calendar items, each having a document.write script.
So, if this is not possible, how can I get around doing it this way?
Bookmarks