www.webdeveloper.com
Results 1 to 6 of 6

Thread: Code screwing up ajax load

  1. #1
    Join Date
    Sep 2007
    Posts
    275

    Code screwing up ajax load

    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>
    Then it uses this on the page to write the date:

    Code:
    <script type="text/javascript">
    document.write(getTFID("2008-05-26 --:--:--"));
    </script>
    Why would this be causing problems, but only through ajax?

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  3. #3
    Join Date
    Sep 2007
    Posts
    275
    Quote Originally Posted by A1ien51
    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:

    Code:
    <script type="text/javascript">
    document.write(getTFID("2008-05-26 --:--:--"));
    </script>
    If this is not the way to do it, how could I get around it?

  4. #4
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    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.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  5. #5
    Join Date
    Sep 2007
    Posts
    275
    Quote Originally Posted by A1ien51
    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?

  6. #6
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Sounds to me you would be better off using an iframe.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles