Click to See Complete Forum and Search --> : URL Preview Assistance


utdream
02-09-2003, 01:41 PM
Greetings,

I'm new to javascript but I've written the following code that lets users preview a typed in URL in a new browser window. It works, but it needs some error checking.

Can anyone help me out with the error checking part or does anyone know where I can find a script that has error checking in it?

Here's the script I wrote:

<SCRIPT LANGUAGE="JavaScript">
<!-- // Start Preview Button
function showPage() {
var opts = 'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,resizable=yes,copyhistory=no,scrollb ars=yes';
var URL = document.form1.Address.value;
var sourcewin = window.open(URL,'Preview',opts)
}
// End Preview Button -->
</SCRIPT>

<form name="form1">
<input name="Address" type="text" id="Address" size="50" maxlength="255">
<input type="button" value="Preview" onClick="showPage()">
</form>

Thank you so much for any help you can offer!

Warm regards,
- utdream

Nevermore
02-09-2003, 01:50 PM
looks like your only problem is that the user has to enter http:// at the start of the txtbox.
If you want a script that does that for them, ill post one in a sec.

Nevermore
02-09-2003, 01:56 PM
Dont worry, if they entered http:// it wont be duplicated. The script checks. I also made the button a submit button, so that you can 'press' it with enter, and made the text box originally read http://. Here it is. If it isnt what u wanted, PM me.

<SCRIPT LANGUAGE="JavaScript">
<!-- // Start Preview Button
function showPage() {
var URL = document.form1.Address.value;
var check = URL.substring(0, 7);
if (check != "http://") {
URL = "http://" + URL;
}
var opts = 'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,resizable=yes,copyhistory=no,scrollb ars=yes';
var sourcewin = window.open(URL,'Preview',opts)
}
// End Preview Button -->
</SCRIPT>

<form name="form1" onSubmit="showPage()">
<input name="Address" type="text" id="Address" size="50" maxlength="255" value="http://">
<input type="submit" value="Preview" >
</form>

Nevermore
02-09-2003, 01:57 PM
oh, and you need to make sure that the opts line IS just one line (i know its just the forum doing that).

utdream
02-09-2003, 02:09 PM
Awesome. Works like a charm. Now that I see what you've done I kinda feel silly for not thinking of it myself! ;)

There's also an error that happens when the user doesn't type in any URL at all, but if they're trying to preview a blank URL they deserve to get an error huh? :rolleyes:

Thank you so much for your help! You rule man!

Warm regards,
- utdream