How to programmatically add JS and CSS resources to ?

前端 未结 2 1855
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 17:23

I need to add programmatically JS and CSS resources to of JSF page. It isn\'t clear how to achieve this. Could someone give a hint or a kickoff e

2条回答
  •  不思量自难忘°
    2020-12-01 18:04

    You can add a script and style resources to a page like this:

    var head = document.getElementsByTagName("head")[0];
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "xxxx.js";
    head.appendChild(s);
    
    s = document.createElement("style");
    s.type = "text/css"
    s.src = "yyy.css";
    head.appendChild(s);
    

    Or, in function form:

    function addScript(path) {
        var head = document.getElementsByTagName("head")[0];
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = path;
        head.appendChild(s);
    }
    
    function addCSSFile(path) {
        var head = document.getElementsByTagName("head")[0];
        var s = document.createElement("style");
        s.type = "text/css";
        s.src = path;
        head.appendChild(s);
    }
    

提交回复
热议问题