简体   繁体   中英

passing in script attributes (data-vars) into head.js?

is there a way to pass script attributes into the loading javascript via head.js?

for example script we need to load is in original form like this :

<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>

so solution we need is something like this :

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() {
}

we have tried this, but it does not work...

No, this is not possible in HeadJS, but you can easily do it yourself

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM