Radiant::loadCSS = (fn, scope) ->
head = document.getElementsByTagName("head")[0]
link = document.createElement("link")
link.setAttribute "href", "/" + ri.context + "/css/" + @obj + ".css"
link.setAttribute "rel", "stylesheet"
link.setAttribute "type", "text/css"
sheet = undefined
cssRules = undefined
if "sheet" of link
sheet = "sheet"
cssRules = "cssRules"
else
sheet = "styleSheet"
cssRules = "rules"
timeout_id = setInterval(->
try
if link[sheet] and link[sheet][cssRules].length
clearInterval timeout_id
clearTimeout timeout_id
fn.call scope or window, true, link #LINE THAT ERRORS OUT!!!
#finally
, 10)
timeout_id = setTimeout(->
clearInterval timeout_id
clearTimeout timeout_id
head.removeChild link
fn.call scope or window, false, link
, 15000)
head.appendChild link
link
以上是我从这里翻译的函数的翻译:Dynamically loading css file using javascript with callback without jQuery
当我在直接javascript中使用它时效果很好,但咖啡版本在ie8 'fn' is null or not an object
以下是该部分的呈现javascript:
timeout_id = setInterval(function() {
try {
if (link[sheet] && link[sheet][cssRules].length) {
clearInterval(timeout_id);
clearTimeout(timeout_id);
return fn.call(scope || window, true, link); //ERROR LINE!!!
}
} catch (_error) {}
}, 10);
无论如何,我是try
和catch
的新手,只是不明白这里出了什么问题。提前谢谢大家!
答案 0 :(得分:1)
我认为这与try / catch关系不大,而且无论出于何种原因都与此有关,fn
在回调执行时没有定义(如错误所示)。在您收到错误的情况下,您确定fn
被传递到loadCSS
吗?