Search code examples
javascriptie8-browser-mode

checking the browser type within function not working in Javascript


In my page, I used javascript function for block with a condition. If the browser is IE8 then the style will be like this or use style within else. But if I used the below condition, the function not works. Anyone please help me to solve this issue

function addnew(type)
{
type=parseInt(type)+1;
 var isOpera, isIE = false;
  if(typeof(window.opera) != 'undefined'){isOpera = true;}
  if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')){isIE = true);
 var mydiv = document.createElement("div");
    mydiv.setAttribute("id",name5);
 if(!IE){
    mydiv.setAttribute("style","width:110px;height:80px;background-image:url(images/transparent.png);float:left;text-align:center;border:1px solid #ccc;");
    }
    else
        {
                mydiv.style.setAttribute('cssText', "width:110px;height:80px;background-image:url(images/transparent.png);float:left;text-align:center;border:1px solid #ccc;");
        }
}

                <input id="addchoice" type=button value="Add New Entry" onclick="addnew(document.forms['addpoll']['choicecount'].value);">

Solution

  • There is a cleaner way to target specific versions of Internet Explorer, Conditional Comments.

    Example:

    <!--[if IE 8]>
    <style type="text/css">
    
     .my-div-class-here { 
         width:110px;
         height:80px;
        ... }
    
    </style>
    <![endif]-->