2013-10-30 56 views
1

有沒有辦法通過head.js將腳本屬性傳遞到加載JavaScript?將腳本屬性(data-vars)傳遞給head.js?

因爲我們需要加載示例腳本原始形式是這樣的:

<script type="text/javascript" charset="utf-8" 
src="script.js" 
data-vast-src="adscript.js" 
data-post-ad-container="content-div" 
data-autoplay="true" 
data-width="600" 
data-height="400"> 
</script> 

這樣的解決方案,我們需要的是這樣的:

head.js("jquery.js", { url: "script.js", charset: "utf-8", data-vast-src: "adcript.js", data-post-ad-container: "content-div", data-autoplay: "", data-width: "600", data-height: "400" }, function() { 
} 

我們已經嘗試過這一點,但它不工作...

回答

0

不,這在HeadJS中是不可能的,但你可以很容易地自己動手做

function addScript(url, attributes) { 
    var s = document.createElement("script"); 

    s.type = "text/javascript"; 
    s.src = url; 

    for (var item in attributes) { 
     s.setAttribute(item, attributes[item]); 
    } 

    var head = document.head || document.getElementsByTagName("head")[0]; 
    head.insertBefore(s, head.lastChild); 
} 

var attributes = { 
    "data-vast-src"   : "adscript.js", 
    "data-post-ad-container": "content-div", 
    "data-autoplay"   : "true", 
    "data-width"   : "600", 
    "data-height"   : "400" 
}; 

addScript("http://...../script.js", attributes);