Pages

Sep 1, 2009

How to show and hide a div in FireFox + Javascript

Normal show and hide code which we normally implement for IE wont work for FireFox. So we go for a generalised code like the below which will work both in FireFox and IE.
The below code show and hide a div on the imagebutton OnClientClick Client side event.

var state = 'none';
function showhide(divtest) {
if (state == 'block') {
state = 'none';
}
else {
state = 'block';
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval("document.all." + divtest+ ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[divtest].display = state;
}
if (document.getElementById && !document.all) {
hza = document.getElementById(divtest);
hza.style.display = state;
}
// To set focus to the div instead of scrolling
div1.scrollIntoView();
}


asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="showhide('div1');return false;" ImageUrl="~/NewImages/Email.png"

No comments: