Forum Moderators: open

Message Too Old, No Replies

Newbie in Javascript

         

ktsirig

9:06 am on Mar 11, 2006 (gmt 0)

10+ Year Member



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?

Robin_reala

12:52 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For stuff like form validation you should always do this server-side incase the user doesn't have JS enabled (you don't want nasty data getting into your DB). Of course, you can then add another client-side layer in JS that does the validation as well. That way users with JS enabled get a faster procedure and your server gets less hits. Any information that's been validated by the client-side script should drop straight through the server-side validation.

Regarding your second question, you could do something like:

links = document.getElementsByTagName("a");
for (i=0; i<links.length; i++) {
links[i].target = "_blank";
}

In English: get all the links in the document, and give them a target of _blank to force them to open in a new window. Of course that's very simplisitic and desn't deal with anchors / new window sizing / etc, but you get the idea.

Bernard Marx

2:30 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[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).

I think what is wanted is something slightly different, Robin. By "popup", ktsirig means a simple popup with defined dimensions and features. When JS is disabled, a new window is still required.

So it's easier to include the target="_blank" in the HTML as the fallback, rather than the main feature.

<a href="mypage.htm" target="_blank" onclick="popup(this.href);return false;">

That is pretty much the standard pattern.
(popup is a custom function where you keep all the fiddly details of the desired window features )