解决css背景图片在ie6不被缓存bug

Filed Under (技术相关) by 刘 鹏 on 10-12-2008

Tagged Under : ,

ie6有个bug,css定义的背景图片不被缓存,今天发现a:hover定义了背景图片,只要鼠标在a上挪动一下就会发送一次请求,这是由于图片没有被缓存,于是上网搜索了下,找到了一个解决方案,很简单,代码如下



  document.execCommand(‘BackgroundImageCache’, false, true);


这个bug存在于ie7以前的版本,因此可以做一个版本的判断

var userAgent = navigator.userAgent.toLowerCase(),

      version = (userAgent.match( /.+(?:rv|it|ra|ie)[/: ]([d.]+)/ ) || [])[1];

if (/msie/.test( userAgent ) && version < 7) {

  try {

   document.execCommand(‘BackgroundImageCache’, false, true);

  } catch(ex) {}

}




参考 http://bokee.shinylife.net/blog/article.asp?id=493