Monday, April 21, 2014

How to disable backspace button as back button in browser ?

Sometimes when we fill online form and by mistake we press the backspace button it take us to the previous page and the data filled by us get lost.so by using the following jquery code we can disable backspace button to work as back button of browser.This  code will disable baspace button as back button if we have clicked outside any input field in the browser and if focus is on textfield or textarea it will behave as usual.


<script type="text/javascript">

$(document).keydown(function(e) {
var element = e.target.nodeName.toLowerCase();
if (e.keyCode === 8) {
   if ((element != 'input' && element != 'textarea') || (element == 'input' && $(e.target).attr("type")== 'radio')|| (element == 'input' && $(e.target).attr("type")== 'checkbox')) {
       return false;
   }
}
});

</script>

Monday, April 14, 2014

what is instaceof in java?

we use instanceof operator to check if an object is an instance of a class, an instance of a subclass,
or an instance of a class that implements a particular interface.It returns true if object is instance of that class
otherwise it returns false.Mostly we use it where we do the the class typecasting.If we use instanceof before typecasting we
will not get the ClassCastException.

for example

Class SuperClass{

}

class Subclass{

}

class MainClass{

public static void main(String [] args){

SuperClass obj1 = new SuperClass();
SubClass obj2 = new SubClass();

if(obj2 instanceof SuperClass){
System.out.println("is obj2 instanceof SuperClass? Ans ="+ obj2 instanceof SuperClass);
obj1 = (SuperClass)obj2;

}


}
}

output : is obj2 instanceof SuperClass? Ans =true

If object is null then instanceof gives always false.

Example

public class InstanceTest{

public static void main(String [] args){

SubClass obj3 =null;
System.out.println("is obj3 instanceof SuperClass? Ans ="+obj3 instanceof SuperClass);

}
}

output : is obj3 instanceof SuperClass? Ans =false