views:

122

answers:

3

First let me acknowledge that I'm in way over my head. Our church website was dropped on me and I'm doing my best to get up to speed after about a 10 year hiatus. Currently undergoing pretty serious chemotherapy, so at least I have lots of time.

I am trying to figure out a way to script an html web page to check for the existence of another web page, and then display that other page if it is found. I found what looks to be a possible solution here but I can't seem to figure out to make it work.

We have a page for youth basketball which, upon loading, should check to see if a "games cancelled" page exists, and if so, display that page. i have some command line scripts ready that will allow the coaches to upload/delete the "cancelled" page, based on weather, via FTP.

I realize I'm probably asking for way too much help here, but please know it would be greatly appreciated.

Thanks very much

NeedyOldNoob

+1  A: 

You do have access to any server-side scripting languages, such as PHP? It might be a lot easier for you to use PHP to do this instead of Javascript. Javascript is client-side, which means it lives in the user's browser and not on the server where your web page will be stored.

PHP is server side, so it will have a much easier time seeing if a file on your server exists or not.

Let me know if you have the ability to use PHP and we can go from there.

Marc W
Thanks for answering. Yes, I do have PHP available, in fact I have been able to get formmail going. Unfortunately I'm further behind the curve on PHP than JS if that's possible, but I'm trying to learn. I appreciate and welcome your thoughts.
NeedyOldNoob
Husky actually answered in the way I would have with PHP. See his answer for what I was going to tell you. =)Doing the Javascript AJAX stuff that some of the other answers suggests seems hacky to me. Also, since this is a church web site it's probably safe to assume that many people are using outdated web browsers and/or have Javascript disabled and/or are not tech savvy enough to enable it. PHP is definitely the better solution for this.
Marc W
Yes, his answer made the light bulb in my head begin flashing. Unfortunately I made a mess of my comment back to him, but I think I have enough information now to get it working. Thanks so much to everybody who replied, and quickly to boot. This was my first visit here, and I was ready to get my shorts flamed off for asking such a noob question. Glad it wasn't that way at all. Thanks again. Maybe if I can get up to speed I can return as a real contributor. Have a great day
NeedyOldNoob
Be sure to choose an answer as accepted so other people who find your question know which one helped you.
Marc W
+3  A: 

First you will need to include the jQuery library:

<script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

Next you will need to check if the page exists:

<script type="text/javascript">
var path = '/THIS_IS_THE_PATH_TO/YOUR_CANCELLED_PAGE.html';

$(function () { 
  $.ajax({ 
    url : path, 
    success : function() { 
        alert('The page was found');
        // redirect to page
        window.location = path;
    }, 
    error : function(xhr, d, e) { 
      if (xhr.status == 404) { 
        // page not found 
        alert('The page was not found');
      } 
    } 
  }); 
});
</script>
cballou
Thanks very much for the help. The information you've provided has filled in some blanks and also cleared up some syntax issues for me. I'm going to see if I can make this work, and then look into PHP alternatives.
NeedyOldNoob
It looks like the 'xhr' object in the error: section doesn't have a 'status' property. I get the intended behaviour by just removing that if check.
Krougan
+2  A: 

PHP is a lot easier to do things like this, it's also a lot faster and will also work with users who don't have Javascript enabled. Also, if you're looking for pages outside of your own domain, Javascript won't allow it because of the same-origin policy.

In the most simple form you could do something like this:

<?php
$page = file_get_contents("http://example.com/page_you_need.html");
if ($page) {
    echo $page;
} else {
    echo "Page does not exist!";
}
?>

Note that you can easily load malicious code using scripts like this, so be sure you trust the page you're trying to read and don't allow any user input in file_get_contents.

Husky
This looks very attractive. If I'm understanding correctly, I could load this instead of the default html page and let it decide what comes next. Would the code below work?<?$page0 = file_get_contents("http://games_cancelled.html");$page1 = file_get_contents("http://default_page.html");if ($page0) { echo $page0;} else { echo $page1;}?>Both of the html files would be local on my web site. The "games_cancelled" file would be uploaded in case of bad weather, then deleted when not needed.Thanks very much for the help
NeedyOldNoob
Whhops... looks like I forgot the formatting guidelines, and I don't know how to fix it
NeedyOldNoob
It's a bit hacky, but yeah that'll probably work :)
Husky