[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Detecting Response status code through JavaScript

Posted on 2007-10-20
6
Medium Priority
?
4,610 Views
Last Modified: 2012-05-05
Is there a way of finding out the status code of the content of an IFRAME to find out if it was dished out by the server or out of the browser cache using JavaScript?
Whenever I Google this I only get stuff back about Ajax but I'm not making an out and call.
0
Comment
Question by:Silas2
  • 3
  • 3
6 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20114639
This is an issue I looked into not too long I ago. I could not figure this out the status code, but what I found is that some browsers fireup the onload event when the frame finishes loading. Thus, you could create an onload event handler. However, even if you do not get a 200 code, like 404(File Not Found) or a 500-(Server Error), the onload event still triggers. The work around would be for the page that you are loading to have a script snippet at the end and once it loads it can call the function originally intended to be the event handler.
0
 

Author Comment

by:Silas2
ID: 20114680
Could I put an OnLoad event handler into the IFRAME, and somehow capture the response status for the IFRAME only?
0
 
LVL 82

Expert Comment

by:hielo
ID: 20114793
You will have the same problem. You must realize that whether the status is 200, 404, or 500, the communication is the same:
client requests resource; server finds resource; server sends resource;
However in the case of a 404, the resource sent is not the resource asked for. The server is nice enough to no "leave you hanging" and instead it sends you an alternate resource, the 404 error page. The same principle the 500 page.
Basically triggering the onload status is like saying, 'the server has finished sending me some resource after I requested a resource'. The whole point of the status code if for the client to determine easily if the resource asked for is the one sent by the server.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Silas2
ID: 20115247
Are you saying that if the response is a 404 error page, I am not going to get any JavaScript anyway as the error page doesn't contain my JavaScript?
I'm really only interested in finding out whether my IFRAME is populated from the browser cache or from the server, can you think of anyway of doing that?
0
 
LVL 82

Accepted Solution

by:
hielo earned 1000 total points
ID: 20116406
Silas2:
I know it is lengthy, but I think I answer your questions and possibly solve your problem.

The 404 error code simply means that the page/resource your requested was not found by the server. However, it does not necessarily mean that you cannot send javascript. Like I said before, when the page you request is not found, the server sends you a different page which normally informs you of the fact that the page does not exist. In all the web servers that I have configured, there is an option/setting that tells you which file it is and where it is located on the system. You could customize that file so that instead of it being an old boring page, the error message appears within your site's template (if any). For example, look at http://www.cnn.com/Silas2.html
That is a custom 404 error page which sends javascript to the client. You could create your own custom 404 and/or 500 error pages with a javascript statement that as soon as the page loads it can call a specific function that you know will exist on all your site. As soon as that function gets executed you will know that the requested resource does not exist. I am 99.99999999% sure that with the custom error page, using any webserver, you can inform the user which resource was being requested but not found. For example, if you look at http://www.google.com/Silas2.html, if they wanted, via javascript could extract the name of the requested resouces and use it to as the argument to some site-wide error handler.

The cache is a different monster altogether. The http status codes(200, 300, 400,etc) are sent to the client by the server whenever there is communication/interaction between the two. If your browser retrieves the page from cache, you will never see a 200, 400, or 500. Maybe a 304 error. If the browser can establish a communication with the server, the browser basically says to the server:
"send me the following file: 'filename", but only if it is newer than the following date: 'date of cached file'." This is known as a "conditioal GET request".  If the file exists on the server but has the same timestamp as the one that the client holds, no data (meaning HTML) is sent to the client, just a mere status code: 304. When the client sees this, it knows that it has the most recent copy stored on cache and will load the iframe or browser from whatever it has in the cache. In the case where it had the file in cache but was not able to establish a connectio (regardless of whether the client system's network is down or the remote server is down), the client (browser) is unable to establish a connection and simply load onto the browser the copy of the file that it has on cache. If you are interested, the meaning of the codes are here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html


To avoid the cache altogether you can "instruct" the browse not to cache your info. Browsers are not obliged to follow said recommendation, but most do. In my experience, I get better results when:
1. Include the following on the html code (NOTE: the meta tags should go within the head before any other tags, including title)

2. Send an http header from the server. For example, in ASP  this can be accomplished as such:
Response.CacheControl = "no-cache";
Response.AddHeader( "Pragma", "no-cache");
Response.Expires = -1;
Whatever your server, it will have an equivalent feature.

How can I tell if the page I requested is cached or not?
Simple: Make sure that in all your pages you include the current system time in UTC format. You could just include it in some meta tag if you do not want it visible. Then, you would also need some javascript to create a system date object and covert it to UTC format. If the difference between the javacript generated UTC time and the one stored in the meta tag is significant, you got a cached page. The difference should typically be a few seconds. The reason for UTC date is because the server may reside in UK, but you may reside in the US. The different timezones will drive you crazy. It is simpler in UTC time because it is like saying, if the whole world synchronized their watches, this is what time it would be right now everywhere!
0
 

Author Comment

by:Silas2
ID: 20156903
I think you're right, anyway I realise really it's the age of the data which is impoortant to the user rather than whether   It was dished out by the browser cache or from  the server.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question