By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

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

P: n/a
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=pag e_request.responseText;
}
}
Can you enlighten me ?
Thanks in advance ;)
Bye.

Nov 20 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
its in case you want to run the script locally.

Nov 21 '06 #2

P: n/a
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.

Nov 21 '06 #3

P: n/a
kd****@gmail.com 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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 21 '06 #4

P: n/a
Randy Webb escreveu:
kd****@gmail.com 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.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 21 '06 #5

P: n/a
Jonas Raoni wrote:
Randy Webb escreveu:
>kd****@gmail.com 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.
Nov 21 '06 #6

P: n/a

Jonas Raoni ha scritto:
Randy Webb escreveu:
kd****@gmail.com 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.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
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.

Nov 21 '06 #7

P: n/a
ASM
wh*****@maktoob.com 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'
{
document.getElementById(containerid).innerHTML=pag e_request.responseText;
}
}

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Nov 21 '06 #8

P: n/a
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.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 22 '06 #9

P: n/a
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.

Nov 22 '06 #10

P: n/a
Jonas Raoni said the following on 11/20/2006 11:14 PM:
Randy Webb escreveu:
>kd****@gmail.com 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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 22 '06 #11

P: n/a
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.

Nov 22 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.