Welcome to WebmasterWorld Guest from 54.144.7.239

Forum Moderators: open

Message Too Old, No Replies

AJAX and http.responseText

     
6:13 pm on Sep 1, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


How do you check if http.responseText is empty or null? I don't want the second part, the else part, to run if it is as per the below:


if(reminderresponse === '' || reminderresponse === null)
{
//do nothing
}else{
window.open ("reminder-display.php?user_id="+reminderresponse,"mywindow","location=1, width=600, height=400, scrollbars=1");
}


I've tried == and ===

Thanks for your help in advance!
7:08 pm on Sept 1, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2606
votes: 0


It will be ==

I think it only returns integers so checking for an empty string won't work.

I think you want

if(reminderresponse == 0)

or maybe

if(reminderresponse == -1)

I am going from memory so I could be mistaken
7:42 pm on Sept 1, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


OK yeah that seems to have worked, thanks!
8:15 pm on Sept 1, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


How do you check if http.responseText is empty or null?


I think it only returns integers...


...that seems to have worked


I think I'm missing something, what is reminderresponse?
9:36 pm on Sept 1, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2606
votes: 0


I assumed it was a variable that he set the http.responseText to
10:52 pm on Sept 1, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2606
votes: 0


penders... it just hit me what you are driving at.

I read this too quick and I even typed it out and still missed it. I thought we were talking about .readyState

Pico I may have led you down the wrong path, it could be that your method is returning an integer but it isn't guarenteed. If it is truly working it was a lucky stumble.

you may want to console.log(reminderresponse) and see what it contains and make sure that == 0 is what you need
11:21 pm on Sept 1, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Yep, that was my confusion. reponseText (as it's name suggests) should be text/string. If the above comparison works then I would guess there is some implicit type conversion going on, and it was quite possibly a 'lucky stumble'? For instance, if reminderresponse contained the string "0", then (reminderresponse == 0) would indeed evaluate to true because of implicit type conversion.
2:00 am on Sept 2, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 26, 2004
posts:3159
votes: 15


You shouldn't serve text files to your visitors, use responseXML instead.

With responseXML you should be able to check for childNodes.

Then you should be able to do something like...
alert(xml_object.childNodes[0].nodeValue.length);


...or count the number of childNodes...
alert(xml_object.childNodes.length);


Also avoid proprietary methods like innerHTML and stick to DOM methods like appendChild or importNode when importing AJAX loaded content in to a page otherwise you're going to deal with a lot of unreliable code and it's associated frustration.

- John