0

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);">
Rithu
  • 1,289
  • 3
  • 19
  • 38
  • What exactly goes wrong? Where does the `IE` variable come from? – Pekka Jan 23 '13 at 10:53
  • var isOpera, isIE = false; if(typeof(window.opera) != 'undefined'){isOpera = true;} if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')){isIE = true); is on the top of the page – Rithu Jan 23 '13 at 10:56
  • But I have a javascript function that contains long coding which contains several variables. For example http://jsfiddle.net/3Sd4W/ This is my coding in which I have to change each variable that supports both IE8 and chrome. Any idea for that. In that addnew function onclick, class, setattribute and some more things are not supported in IE8 – Rithu Jan 23 '13 at 10:58
  • Well, you'll have to debug it or tell us what exactly goes wrong with it. Anyway, if you can, use Conditional Comments instead – Pekka Jan 23 '13 at 10:59

2 Answers2

2

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]-->
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0
function addnew(type)
{
var isOpera, isIE = false;
       if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
  var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
   if (ieversion==8)
    isIE = true;
   }
if(isIE)
{
//code for IE
}

else
{
//code for other browsers
}
Rithu
  • 1,289
  • 3
  • 19
  • 38