AJAX window.location.href.indexOf("http")==-1)

W

whisher

Hi.
Sorry to disturb you but I can't
figure out its use in this snippet
(Found in many scripts about Ajax)
function loadpage(page_request, containerid)
{
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))
{
document.getElementById(containerid).innerHTML=page_request.responseText;
}
}
Can you enlighten me ?
Thanks in advance ;)
Bye.
 
K

kday33

window.location.href.indexOf("http")==-1

I haven't seen this before, but this part of the statement will be true
if the url of the requesting page does not contain the string "http".
Shimmyshack is probably right, because if you're running the script in
an HTML page locally on your computer, then it won't have "http" in the
URL, it'll be something like "C:/..." whatever.
 
R

Randy Webb

(e-mail address removed) said the following on 11/20/2006 8:49 PM:
window.location.href.indexOf("http")==-1

I haven't seen this before, but this part of the statement will be true
if the url of the requesting page does not contain the string "http".
Shimmyshack is probably right, because if you're running the script in
an HTML page locally on your computer, then it won't have "http" in the
URL, it'll be something like "C:/..." whatever.

And if you are running it locally the readyState will never be 200.
 
J

Jonas Raoni

Randy Webb escreveu:
(e-mail address removed) said the following on 11/20/2006 8:49 PM:
[...]
And if you are running it locally the readyState will never be 200.

Also checking for the constant "200" isn't nice in my opinion, because
what defines the status is just the first digit of the code, the others
act as a kind of sub-status.
 
R

Richard Cornford

Jonas said:
Randy Webb escreveu:
(e-mail address removed) said the following on 11/20/2006 8:49 PM:
[...]
And if you are running it locally the readyState will never be 200.

Also checking for the constant "200" isn't nice in my opinion, because
what defines the status is just the first digit of the code, the
others act as a kind of sub-status.

Are you proposing that HTTP status 200 (OK) should be handled the same
way as 201 (Created), 204 (No Content) or 202 (Accepted)?

Richard.
 
W

whisher

Jonas Raoni ha scritto:
Randy Webb escreveu:
(e-mail address removed) said the following on 11/20/2006 8:49 PM:
[...]
And if you are running it locally the readyState will never be 200.

Also checking for the constant "200" isn't nice in my opinion, because
what defines the status is just the first digit of the code, the others
act as a kind of sub-status.
Thanks a lot buddies.
It's quite clear to me.
But I also found this Ajax library
and to check the status it's use
a boolean var.
function XHConn()
{
var xmlhttp, bComplete = false;
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { try { xmlhttp = new XMLHttpRequest(); }
catch (e) { xmlhttp = false; }}}
if (!xmlhttp) return null;
this.connect = function(sURL, sMethod, sVars, fnDone)
{
if (!xmlhttp) return false;
bComplete = false;
sMethod = sMethod.toUpperCase();

try {
if (sMethod == "GET")
{
xmlhttp.open(sMethod, sURL+"?"+sVars, true);
sVars = "";
}
else
{
xmlhttp.open(sMethod, sURL, true);
xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");

xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
xmlhttp.onreadystatechange = function(){
//HERE -------------!bComplete-------------
if (xmlhttp.readyState == 4 && !bComplete)
{
bComplete = true;
fnDone(xmlhttp);
}};
xmlhttp.send(sVars);
}
catch(z) { return false; }
return true;
};
return this;
}
var myConn = new XHConn();

if (!myConn) alert("XMLHTTP not available. Try a newer/better
browser.");

var fnWhenDone = function (pXML) { alert(pXML.responseText); };

myConn.connect("test.txt", "GET","",fnWhenDone);


Is it the same case or not ?

Bye.
 
A

ASM

(e-mail address removed) a écrit :
Hi.
Sorry to disturb you but I can't
figure out its use in this snippet
function loadpage(page_request, containerid)
{
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))

if XMLHttpRequested file is ready ( =4 )
and
- all was OK on server ( =200)
- or file actually displayed *is* from http
in this case : all was not OK
and error (responseText) will be shown in div 'contaierid'
 
J

Jonas Raoni

Richard Cornford escreveu:
Jonas Raoni wrote:
Are you proposing that HTTP status 200 (OK) should be handled the same
way as 201 (Created), 204 (No Content) or 202 (Accepted)?

No, you handle them the way you want, I just said that if the response
doesn't have the status 200, it doesn't mean that it failed.
 
S

shimmyshack

No, you handle them the way you want, I just said that if the response
doesn't have the status 200, it doesn't mean that it failed.
I'm with you Jonas,

I often use 204 (no content) to quickly stop the browser in its tracks
whilst still having a client-server interaction, and 206 (partial
content) to add to previously buffered output from the server.

At the moment many of the checks and balances buried inside the
libraries only reflect a basic implementation and imho as ajax gets
more mature, we will see these libraries reflecting the full rfc's in
many of these areas, such as HTTP.
 
R

Randy Webb

Jonas Raoni said the following on 11/20/2006 11:14 PM:
Randy Webb escreveu:
(e-mail address removed) said the following on 11/20/2006 8:49 PM:
[...]
And if you are running it locally the readyState will never be 200.

Also checking for the constant "200" isn't nice in my opinion,

Then what *should* you be checking for? 2* ?
because what defines the status is just the first digit of the code, the others
act as a kind of sub-status.

And in this case, you want 200 and 200 only.
 
S

shimmyshack

This script was part of a library, Jonas and I are making the point
that as AJAX matures the complexity of this part of the js that checks
for response codes should start reflect the full structure of the RFC
where 200 _isn't_ always what you want or what you get.

For instance, a 304 header check could be implemented here, if
somewhere else in the code caching is being used, so that you get a
benefit in using AJAX, if each time you click a button you get the same
200 with response body, this is hardly using AJAX to its full, however
if you have implemented some form of caching, then the headers sent
could be a bit more advanced and the codes back could save you time in
both receiving the data, parsing it and rendering it.

I would personally prefer it if libraries didnt just assume that 200 is
the only code out there that is useful to AJAX.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top