Click to See Complete Forum and Search --> : Validate a URL before loading it!


tiggerific
06-18-2003, 09:23 AM
I know this will be an easy one for all you experiened scripters, but I'm having a little trouble with it.

My page contains an iframe which loads the source file based on the variable passed to the page. Before I update the iframe with the source, I want to check that the variable passed will generate a valid (live) url to be loaded, then go ahead and load it.

If not valid, I will load my personal 'file not found' page or something similar.

Hope you can help sort this one out quickly.

Thanks.

Robyn.

gsb
06-18-2003, 10:16 AM
Not easily accomplished in JavaScript, IMHO.

I'd use php to test and signal the page if the URL exists or not.
Youu set up a php script thet takes the URL as a parameter.
It checks the URL and returns a .gif image of size 1x1 or 2x2 where 1x1 is True and 2x2 is false.
In your JavaScript the user enters the parameters and calls a check routine upon submit.

<SCRIPT LANGUAGE="JavaScript">

var imgObj=null, myTimer=null, attempts=3

function requestURLcheck() {
if(myTimer==null) {
var flag=new Date()
imgObj=new Image()
imgObj.onload=function(evt){processTrueFalse()}
imgObj.src= "YOUR_SITE/truefalse.php?flag="+flag.valueOf()+"&URL="+USERS.URL
myTimer=setTimeout("noResponse()",4999); // 10 seconds for timeout
}
}

function processTrueFalse() {
if(myTimer!=null){clearTimeout(myTimer);myTimer=null}
if(imgObj.width>1) IFRAME.src=USERS.URL
else IFRAME.src=YOUR_SITE/CUSTOM.NOT.FOUND.URL
imgObj=null
}

function noResponse() {
imgObj=myTimer=null
if(--attempts)requestTrueFalse()
else alert("Server Error 1002 - Timeout. ")
}

</SCRIPT>

tiggerific
06-18-2003, 12:40 PM
Hmm. Thanks for the advice, but this is getting a bit more complicated than I imagined!
Isn't there some way I can just do a test load or a pre-load(maybe into an invisible iframe) and see if it returns an error then set my variables accordingly?

Robyn

pyro
06-18-2003, 12:48 PM
I would use PHP as well. Is that an option for you?

tiggerific
06-18-2003, 12:52 PM
Unfortunately, no, but only because I have no knowledge of it. My host supports it, but I've never used it and I'm only managing to fumble my way through JavaScript - just enough to achieve what I want.

Jona
06-18-2003, 12:55 PM
Originally posted by tiggerific
Unfortunately, no, but only because I have no knowledge of it.

Well, if you're willing to learn we can help you. You might want to post in the PHP forum, unless pyro objects. ;)

Jona

pyro
06-18-2003, 01:04 PM
Nope, no objections. Post in the PHP forums, and I will give you a cut and paste script with comments in the PHP to explain it... :)

tiggerific
06-18-2003, 01:09 PM
well, i don't mind the learning part - i usually just work on trial and error until i get things working :confused: .

if i was going to post in the php forum i should probably check out some basic tutorials or something first so i can at least ask the questions properly?

i don't know - what started off as a simple If/Else statement seems to be becoming a major programming task :rolleyes:

I'll have a bit of a look into php basics and see what i might be up against i think.

pyro
06-18-2003, 01:11 PM
Originally posted by tiggerific
I'll have a bit of a look ... and see what i might be up against i think. Under 20 lines of PHP code... :)

tiggerific
06-18-2003, 01:11 PM
must have been posting at the same time pyro.

ok, i'll post my original question in the php forum.

posting away...

(thanks for your help guys)