JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2061
Number of posts: 5164

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
javascript loads specific page else ALERT Posted by mgtsa on 11 Apr 2004 at 2:55 AM
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: ----------------

<html>
<head>

<script ="javascript">

function(doThis)
{
refvar = document.form1.refnum.value;
}
</script>
</head>


<body>

<form name="form1">
refnum:
<input type="text" id="refnum" name="refnum" value=""><BR>
</form>


<script type="text/javascript">

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

<BR>
<BR>
<a href="#" onClick="findpage(); return false">GET MY PAGE</a>


</body>
</html>
Report
Re: javascript loads specific page else ALERT Posted by zibadian on 11 Apr 2004 at 3:32 AM
: 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: ----------------
:
: <html>
: <head>
:
: <script ="javascript">
:
: function(doThis)
: {
: refvar = document.form1.refnum.value;
: }
: </script>
: </head>
:
:
: <body>
:
: <form name="form1">
: refnum:
: <input type="text" id="refnum" name="refnum" value="">
: </form>
:
:
: <script type="text/javascript">
:
: function findpage(refUrl)
: {
: refvar = document.form1.refnum.value;
: refUrl = (refvar + ".htm")
: var mypopup2=window.open((refUrl),"win4",'width=300,height=400');
: mypopup2.focus();
: }
: </script>
:
:
:
: <a href="#" onClick="findpage(); return false">GET MY PAGE</a>
:
:
: </body>
: </html>
:
Here is a trick to check if a page exists. It might only work for IE 5+, but at least it's something:
  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");
  }

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.
Report
Re: javascript loads specific page else ALERT Posted by mgtsa on 11 Apr 2004 at 1:27 PM
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

Report
Re: javascript loads specific page else ALERT Posted by Weirdofreak on 12 Apr 2004 at 2:39 AM
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:

<html>
<head>
<script type="text/javascript">

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();
}
</script>

</head>


<body>

<form name="form1">
refnum:
<input type="text" id="refnum" name="refnum" value=""><BR>
</form>


<BR>
<BR>
<a href="#" onClick="findpage(); return false">GET MY PAGE</a>


</body>
</html> 




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.