简体   繁体   中英

IE8 warning: CSS styling with JavaScript for -moz-linear-gradient

I am using JavaScript to style a control element.

controlText.style.background = "-moz-linear-gradient(center top , #6D8ACC, #7B98D9)";
controlText.style.background = "-webkit-gradient(linear, 0% 0%, 0% 100%, from(#6D8ACC), to(#7B98D9))";

Now, this works fine except in IE8, where I get a Invalid argument warning.

Can someone please let me know, how to modify the code to get this working on IE without warnings/errors?

Regards
Nikhil Gupta

Solution : Thank you, for the inputs, I solved it with jQuery.browser.msie like this:

if (jQuery.browser.msie) {
  controlText.style.background = "#7B98D9";
} else {
  controlText.style.background = "-moz-linear-gradient(center top , #6D8ACC, #7B98D9)";
  controlText.style.background = "-webkit-gradient(linear, 0% 0%, 0% 100%, from(#6D8ACC), to(#7B98D9))";
}

You should only set those before checking if it's Internet Explorer.

IE doesn't support gradients so it gives the warning.

EDIT:

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

source: http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx

you can use this snippet to check for IE:

function isIE() {
    return (navigator.userAgent.toLowerCase().indexOf("msie")!=-1);
}

Use filters for IE8 effects. In this case, Gradient Filter should work.

For example, I use this style for a cross-browser gradient effect in blue

.blue {
    color: #d9eef7;
    border: solid 1px #0076a3;
    background: #ground: -webkit-gradient(linear, left top, left bottom, from(#4A83D6), to(#185498));
    background: -moz-linear-gradient(top,  #4A83D6,  #185498);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#4A83D6', endColorstr='#185498');
}

Better to use jQuery to set a class name and put your cross browser gradient CSS in a class. (keep your design separate from your code!)

Use this page for your gradients. http://www.colorzilla.com/gradient-editor/

    controlText.addClass('myGrad gradient');


    .myGrad{
        background: #6d8acc; /* Old browsers */
        /* IE9 SVG, needs conditional override of 'filter' to 'none' */
        background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzZkOGFjYyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM3Yjk4ZDkiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
        background: -moz-linear-gradient(top,  #6d8acc 0%, #7b98d9 100%); /* FF3.6+ */
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6d8acc), color-stop(100%,#7b98d9)); /* Chrome,Safari4+ */
        background: -webkit-linear-gradient(top,  #6d8acc 0%,#7b98d9 100%); /* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(top,  #6d8acc 0%,#7b98d9 100%); /* Opera 11.10+ */
        background: -ms-linear-gradient(top,  #6d8acc 0%,#7b98d9 100%); /* IE10+ */
        background: linear-gradient(to bottom,  #6d8acc 0%,#7b98d9 100%); /* W3C */
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6d8acc', endColorstr='#7b98d9',GradientType=0 ); /* IE6-8 */

    }

    <!--[if gte IE 9]>
      <style type="text/css">
        .gradient {
           filter: none;
        }
      </style>
    <![endif]-->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM