Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用yepnope,我如何知道css资源是否已成功加载?_Javascript_Css_Yepnope - Fatal编程技术网

Javascript 使用yepnope,我如何知道css资源是否已成功加载?

Javascript 使用yepnope,我如何知道css资源是否已成功加载?,javascript,css,yepnope,Javascript,Css,Yepnope,我正在开发一个webapp,供公司内部使用,该公司拥有严格且不断发展的防火墙 我正在使用yepnope.js/Modernizr.load从CDN加载带有回退的js和css文件 但我如何知道css是否成功加载?有没有办法测试css的成功加载和激活?多亏@Morpheus的提示,这里有一个解决方案: 首先,为了不依赖任何jQuery等,我们需要定义2个函数: 要获取元素的css属性,请执行以下操作: 从 另一种方法是创建隐藏的DOM元素并测试其css属性: 从 也许这就是你要找的@莫菲斯,就是这样

我正在开发一个webapp,供公司内部使用,该公司拥有严格且不断发展的防火墙

我正在使用yepnope.js/Modernizr.load从CDN加载带有回退的js和css文件


但我如何知道css是否成功加载?有没有办法测试css的成功加载和激活?

多亏@Morpheus的提示,这里有一个解决方案:

首先,为了不依赖任何jQuery等,我们需要定义2个函数:

要获取元素的css属性,请执行以下操作: 从

另一种方法是创建隐藏的DOM元素并测试其css属性: 从


也许这就是你要找的@莫菲斯,就是这样!我应该能够用这种技术编写一个yepnope实现。那我就分享吧。
function getStyle(oElm, strCssRule) {
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
},
function test_css(element_name, element_classes, expected_styles, rsrc_name, callback) {
  // create an element of given type
  var elem = document.createElement(element_name);
  // immediately hide it
  elem.style.display = 'none';
  // give it the class(es)
  for (var i = 0; i < element_classes.length; i++) {
    elem.className = elem.className + " " + element_classes[i];
  }
  // and add it to the DOM
  document.body.appendChild(elem);

  // test the given properties
  // it happens (~1/3 times in local)
  // that the css takes a few ms to apply.
  // so we need a function that we can try directly first
  // and retry again later in case of failure
  var handle = -1;
  var try_count = 0;
  var test_function = function() {
    var match = true; // so far
    try_count++;
    for (var key in expected_styles) {
      console.log("[CSS loader] testing " + rsrc_name + " css : " + key + " = '" + expected_styles[key] + "', actual = '" + get_style_of_element(elem, key) + "'");
      if(get_style_of_element(elem, key) === expected_styles[key]) {
        match = match && true;
      } else {
        console.error("[CSS loader] css " + rsrc_name + " test failed (not ready yet ?) : " + key + " = " + expected_styles[key] + ", actual = " + get_style_of_element(elem, key) );
        match = false;
      }
    }

    if (match === true || try_count >= 3) {
      if (handle >= 0)
        window.clearTimeout(handle);
      // remove our test element from the DOM
      document.body.removeChild(elem);
      if (!match)
        console.error("[CSS loader] giving up on css " + rsrc_name + "..." );
      else
        console.log("[CSS loader] css " + rsrc_name + " load success !" );
      callback(rsrc_name, match);
    }
    return match;
  }

  // use and program the function
  if(! test_function() ) {
    console.info("" + rsrc_name + " css test failed, programming a retry...");
    handle = window.setInterval(test_function, 100);
  }
}
{
  load: { 'bootstrap-css': 'http//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css' },
  callback: function (url, result, key) {
  // here for bootstrap, I test presence of .span1 class
  test_css('span', [ 'span1' ], { 'width': '60px' }, function(match) {
    if( match )  {
      // it worked !
    }
    else {
      // ... fallback on another CDN or local rsrc...
    }
  }
}