Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

javascript loads specific page else ALERT

mgtsamgtsa Posts: 2Member
Hi!

Im REALLY new at Javascript - been browsing these forums and reading up like crazy for ony two weeks, to try and learn enough for a project i'm working on.

THE QUESTION IS:

I want the user to be able to type in a number (eg: abc_123) that will launch a popup containing the page (eg: abc_123.htm). I've managed to get this right by adapting a password script(where password= pagename.htm). That was quite easy.

BUT now i need to do this: IF they type in a filename that doesnt exist, it must either load a page saying "the number you have entered..." or cause an ALERT to popup. I've read that one can customise error pages and upload them to the server, but then that page would appear for ANY other error on the site too.

Is there a way to do this? I would guess I need to use an IF (else) statement. Is there any way javascript could check to see if the page exists (then load it), otherwise popup a custom page or ALERT

There are going to be about 15 code numbers that the user could type in (and these will be changing from week to week). I was hoping there was a way i could use javascript to check the files in the directory and act accordingly.

I guess one way would be to put the 15 numbers in an array (?), and use javascript to check that way, but that means i'll have to keep changing the array every few days - which i'm trying to avoid. Could i place that array in an exernal .js file to make it easier to update?

But ideally i'd prefer not to have to update every few days...


I've inserted my code below if needed.

Any help would be MUCH appreciated!

(AN excited-about-javascript newbie)



------- CODE: ----------------






function(doThis)
{
refvar = document.form1.refnum.value;
}







refnum:







function findpage(refUrl)
{
refvar = document.form1.refnum.value;
refUrl = (refvar + ".htm")
var mypopup2=window.open((refUrl),"win4",'width=300,height=400');
mypopup2.focus();
}






GET MY PAGE




Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi!
    :
    : Im REALLY new at Javascript - been browsing these forums and reading up like crazy for ony two weeks, to try and learn enough for a project i'm working on.
    :
    : THE QUESTION IS:
    :
    : I want the user to be able to type in a number (eg: abc_123) that will launch a popup containing the page (eg: abc_123.htm). I've managed to get this right by adapting a password script(where password= pagename.htm). That was quite easy.
    :
    : BUT now i need to do this: IF they type in a filename that doesnt exist, it must either load a page saying "the number you have entered..." or cause an ALERT to popup. I've read that one can customise error pages and upload them to the server, but then that page would appear for ANY other error on the site too.
    :
    : Is there a way to do this? I would guess I need to use an IF (else) statement. Is there any way javascript could check to see if the page exists (then load it), otherwise popup a custom page or ALERT
    :
    : There are going to be about 15 code numbers that the user could type in (and these will be changing from week to week). I was hoping there was a way i could use javascript to check the files in the directory and act accordingly.
    :
    : I guess one way would be to put the 15 numbers in an array (?), and use javascript to check that way, but that means i'll have to keep changing the array every few days - which i'm trying to avoid. Could i place that array in an exernal .js file to make it easier to update?
    :
    : But ideally i'd prefer not to have to update every few days...
    :
    :
    : I've inserted my code below if needed.
    :
    : Any help would be MUCH appreciated!
    :
    : (AN excited-about-javascript newbie)
    :
    :
    :
    : ------- CODE: ----------------
    :
    :
    :
    :
    :
    :
    : function(doThis)
    : {
    : refvar = document.form1.refnum.value;
    : }
    :
    :
    :
    :
    :
    :
    :
    : refnum:
    :
    :
    :
    :
    :
    :
    : function findpage(refUrl)
    : {
    : refvar = document.form1.refnum.value;
    : refUrl = (refvar + ".htm")
    : var mypopup2=window.open((refUrl),"win4",'width=300,height=400');
    : mypopup2.focus();
    : }
    :
    :
    :
    :
    : GET MY PAGE
    :
    :
    :
    :
    :
    Here is a trick to check if a page exists. It might only work for IE 5+, but at least it's something:
    [code]
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.load(refUrl);
    if (xmlDoc.parseError.errorCode == -2146697210)
    {
    alert("Page does not exist!");
    } else {
    alert("Page exists. Continueing to load it");
    }
    [/code]
    This loads the page as a XML-file into the XML-parser. It will always return an errorCode, since your page isn't valid XML. The code above is returned if the page isn't found.
  • mgtsamgtsa Posts: 2Member
    Thank you zibadian!

    Its all working! - it took me a couple of times to work out where to cut and paste it, but it works GREAT - JUST what i needed. I'll test it in various browsers to be on the safe side.

    Thanks again for the help. I'll definitely come back to these forums if i need to learn something again (probably soon), and who knows, maybe one day i'll even be able to help someone else !

    Thanks :-D

  • WeirdofreakWeirdofreak Posts: 439Member
    I'm pretty sure he's right, it probably won't work except in IE.

    However, it's the easiest way to do it. The only other ways that I can think of (apart from the one you're trying to avoid) would be to put the pages in a seperate folder and set the 404 page for that, if you can, or to read the HTML of the page, and if it matches your 404 set it to a different page:

    [code]



    function findpage(refUrl)
    {
    refvar = document.form1.refnum.value;
    refUrl = (refvar + ".htm")
    var mypopup2=window.open((refUrl),"win4",'width=300,height=400');
    var pagecode = mopopup2.document.innerHTML;
    var code404 = "[HTML of your 404 page goes here]";
    if (pagecode == code404)
    mypopup2.location.href = 'nonexistantpage.html';
    mypopup2.focus();
    }








    refnum:









    GET MY PAGE



    [/code]
Sign In or Register to comment.