PDA

View Full Version : Newbie Javascript questions


ktsirig
03-11-2006, 07:02 AM
Hello all!
I am starting to use JS a couple of days now and mainly I am interested in using it in form validation and to create pop-ups.
I have 2 questions :

1) If a user doesn't have JS enabled, how will I check for empty fields in the form and show an alert box that prompts for corrections?

2) Also, I have seen sites that have the following : There is a link somewhere which works like this : [a] if you have JS enabled, it "recognizes" it and gives a pop-up window when clicked whereas [b] if you don't have JS enabled, it simply acts like a link and opens a new window (not a poup-up one).

Because I am a bit confused, I am staring to think that maybe JS causes more problems than it solves... Should a programmer use it or ,for instance, do a form validation in another page and don't count on the user having JS enabled?

coothead
03-11-2006, 07:25 AM
Hi there ktsirig,

If a user doesn't have JS enabled, how will I check for empty fields in the form and show an alert box that prompts for corrections?
This can be achieved server-side (PHP etc).

Also, I have seen sites that have the following : There is a link somewhere which works like this : [a] if you have JS enabled, it "recognizes" it and gives a pop-up window when clicked whereas [b] if you don't have JS enabled, it simply acts like a link and opens a new window (not a poup-up one).
Here is an example...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>popup example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
<!--
var mywindow;
var features='width=500,height=400,left=200,top=200'
function smallWindow(url) {
if(mywindow) {
mywindow.close();
}
mywindow=window.open(url,'',features);
mywindow.focus();
}

//-->
</script>

</head>
<body>

<div>
<a href="http://www.google.com" onclick="smallWindow(this);return false">google</a>
</div>

</body>
</html>
The 'return false' that is highlighted in the link will prevent the link acting normally, if javascript is enabled.

Kravvitz
03-11-2006, 02:29 PM
1) Regardless of whether you use JavaScript to validate a form or not, you should always use a server-side language to validate it, since there is no way to make sure that JavaScript is enabled in every user's browser.

2) I suggest you read this: http://www.alistapart.com/articles/popuplinks/