Click to See Complete Forum and Search --> : Detecting if a page exists w/ JavaScript


Dudsmack
11-16-2003, 03:32 PM
Is it possible to use JavaScript to detect if a page exists on a server?

Basically, if the page exists the user would be forwarded to it, but if the page doesn't exist my own custom "404" page would be loaded. Before you go thinking this wouldn't work as a replacement for a 404 page, I know. I've got a simple password script that loads page based on the entered password instead of encrypting a password into the page.

I was playing with having the page load into a DIV, and if it loads then forward the user to that page, but the problem is it loads the ENTIRE page into the DIV which takes extra time. Perhaps it is possible to load only a section of the page into the DIV and then check the DIV for the content? Is it even possible to read just a section of the page (maybe the title or something)?!?

Any ideals?

Jona
11-16-2003, 08:54 PM
Um, if the page doesn't exist, then you get a 404... You can set it up to redirect to the main page automatically on your 404 page, but JavaScript has no access to the server...

[J]ona

Jona
11-16-2003, 08:55 PM
Although, come to think of it, you could use an IFrame, and check to see if its title is equal to "404," and if so, alert "Page does not exist," then return false.

[J]ona

dondawg
08-31-2005, 02:26 PM
But different.

Is it possible to display two different images based on if a particular server is up and running?

Here is the scenario.

I am hosting my own personal web page with a hosting service (Mecca Hosting), yet I also use a dynamic DNS resolution embeded within a page hosted by Mecca to point to a streaming server running on my desktop.

What I would like to do is have something like the below in the Mecca hosted pages HTML.

if (mydesktop server) is running
then
display image1.jpg
else
display image2.jpg
endif

Hopefully this would give a static webcam offline image if the webcam server is not running or any particular image from the running server is not available.

Thanks!!!

Don

Jona
08-31-2005, 05:11 PM
It’d be best to do this with a server-side language. You can do it with JavaScript, but it’s not as reliable (and doesn’t work in as many browsers). Do you have a server-side language available, such as PHP?

Ultimater
08-31-2005, 05:34 PM
Although, come to think of it, you could use an IFrame, and check to see if its title is equal to "404," and if so, alert "Page does not exist," then return false.

Then you would run into a problem of cross-server security, assuming that the orginal poster is including hyperlinks aimed at other servers, and you wouldn't be able to use javascript to check the title of the IFrame(s). Even if you went as far as using an ".HTA" application to avoid cross-server security issues, what if the page isn't found on the mysterious server and the server spits-up a custom 404 page that doesn't have the text "404" in it's title? What if the page is found and it's title reads, "call us! 404-874-7926". Even worse, if the user stops the HTA page loading as an application... The only answer is to go server-side.

Jona
08-31-2005, 06:13 PM
Heh, Ultimater, I posted that a long time ago. ;)

I think you might be able to get through cross-server security, but that’s a maybe. Even if possible, a JavaScript solution wouldn’t be as stable as a server-side one.

Ultimater
08-31-2005, 06:36 PM
Nope, you can't spoof the security, give this a try:
testing123.htm

<iframe src="http://www.yahoo.com"></iframe>
<script type="text/javascript">
onload=function(){
var e=frames[0];
alert(e?"Page has finished loading":"preload stage...")
alert(e.document.title)
}
</script>

You should recieve a single alert "Page has finished loading" and then get an "access denied" error.

Now simply change the file's extension to "hta":
testing123.hta
and you get two alerts:
"Page has finished loading"
"Yahoo!"

Jona
08-31-2005, 07:00 PM
Sorry, I meant remotely accessing the document without using an IFrame. Also, instead of checking the title, you can check the HTTP status code the document returns in the headers it sends back to the client.

Ultimater
08-31-2005, 07:18 PM
That would still involve using some server side kind of script...

HTTP Status Code: HTTP/1.1 200 OK

web-sniffer.net results (http://web-sniffer.net/?url=http%3A%2F%2Fwww.yahoo.com&submit=Submit&http=1.1&gzip=yes&type=GET&ua=Mozilla%2F5.0+%28Windows%3B+U%3B+Windows+NT+5.1%3B+en-US%3B+rv%3A1.7.8%29+Gecko%2F20050511+Firefox%2F1.0.4+Web-Sniffer%2F1.0.21) on "http://www.yahoo.com"

HTTP Status Code: HTTP/1.1 404

web-sniffer.net results (http://web-sniffer.net/?url=http%3A%2F%2Fwww.yahoo.com%2F32434&submit=Submit&http=1.1&gzip=yes&type=GET&ua=Mozilla%2F5.0+%28Windows%3B+U%3B+Windows+NT+5.1%3B+en-US%3B+rv%3A1.7.8%29+Gecko%2F20050511+Firefox%2F1.0.4+Web-Sniffer%2F1.0.21) on "http://www.yahoo.com/32434" (of coarse a 404)

Of coarse you wouldn't use web-sniffer.net to give you the connection result but rather a fresh server-side script that would return for instance only "404" or "200" etc... Once you have one of these scripts up and working, you'd best execute it by sending it a querystring and having it return javascript code as if it were an external javascript file and dynamically include it into your page at run-time in order to check if the URL exists. In that case you would want it to return for instance pageConnection="404" or pageConnection="200" etc... Mmmmm sounds like a fun script to make.