簡體   English   中英

使用yepnope,如何知道CSS資源是否已成功加載?

[英]Using yepnope, how can I know if a css resource was loaded successfully?

我正在開發一個Web應用程序,供在具有嚴格(且不斷發展)的防火牆的公司內部使用。

我正在使用yepnope.js / Modernizr.load從CDN加載帶有備用的js和css文件。

但是,如何知道CSS是否成功加載? 有沒有一種方法可以測試CSS的成功加載和激活?

感謝@Morpheus的提示,這是一個解決方案:

首先,為了不依賴任何東西(jQuery等),我們需要定義2個函數:

  • 一個獲取元素的css屬性的方法:

來自http://robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/

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;
},
  • 另一個創建隱藏的DOM元素並測試其CSS屬性:

來自https://stackoverflow.com/a/8122005/587407

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);
  }
}

注意,由於css的延遲加載,先前的功能比較棘手。

我們現在可以像這樣使用yepnope:

{
  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...
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM