Sunday, July 22, 2007

Nested tags and JS concepts..

Last week i come across some weird situations while coding,so here i just want to share those with you people.

1.Nested Form tags. (never do)

Every Java developer knows that what ever appears on the browser is an out come of Java script and HTML.So to pass the values from HTML to a specific URL's we mostly use form tags.Forms are made of text boxes, check boxes, radio buttons, drop-down lists, and other input fields.

The representation of for tag is

to open and
the close of the tag.
So the code seems to be as
<'form'>
//your HTML code
<'/form'>

so the problem i regularly faced is the nested of the form which you shouldn't do while coding.

//Its a wrong way to handle


<'form'>(Parent form)

//your HTML code-1

<'form'>(child form)

//your HTML code-2

<'/form'>(child form closed)

<'/form'>()


(NOTE:- there shouldn't single cot in the tag *'*. I introduced ' because other while it accepts as a form tag.)
Any how you will never face the problem in Firefox i don't know how it able to find the closed parent form object before starting the child form.But IE won't allow you to do this in it ,it throws a JS error.
So in above image i have created a request form which ends at the last of the page in between i used another Task form which is present in a div -shown as Task tab .now when i click that tab it throws a JS error which is not shown in Firefox.

Here are some cases when one can do this mistake mostly
  • When your are including one jsp in another jsp.
  • When you are hiding and showing the div and the div has a form .
  • while constructing through iframe.
well here i said few of the possible cases.


2.Difference between "var a=3" and "a=3" in JavaScript :-
Every Java developer knows the difference between "var a" and "a" i.e if you are declaring var a=3 ,'a' will be assigned locally; but when you declare 'a=3' a will be declared Globally.This is theoretically i to know but practically no, so one of my college explain me practically which as follows,

Case1:
<'script'>
var a =3;
function 123{

a = 4;
alert1('a='+a); //alert1

}
alert2('a='+a);
<'/script'>

Case2:
<'script'>
a =3;
function 123{

a = 4;
alert1('a='+a); //alert1

}
alert2('a='+a);
<'/script'>

Case1:-So suppose here u called a function 123 now what would u expect those alert results. so the ans is 4 & 4 respectively .So here if you define a variable a as var a=3 it will lost its value if any function called over writes 'a' value.


Case2:-So now what you expect the result here.Here it will be 4 & 3 respectively.Once you define a variable global then it will retain its value globally through out the script even if you used inside any function it wont lost its value out of the function.

No comments: