动态加载css、js引用

在js代码中动态的加载js、css文件的引用

    function addJsCssByLink(type,url)
    {
        var doc=document;
        if(type="js")
        {
            var link=doc.createElement("script");
            link.setAttribute("type", "text/javascript");
            link.setAttribute("src", url);
     
            var heads = doc.getElementsByTagName("head");
            if(heads.length)
                heads[0].appendChild(link);
            else
                doc.documentElement.appendChild(link);
         }
        if(type="css")
        {
            var link=doc.createElement("link");
            link.setAttribute("rel", "stylesheet");
            link.setAttribute("type", "text/css");
            link.setAttribute("href", url);
     
            var heads = doc.getElementsByTagName("head");
            if(heads.length)
                heads[0].appendChild(link);
            else
                doc.documentElement.appendChild(link);
         }
    }

 

posted on 2014-01-23 21:07  ※WYF※  阅读(491)  评论(0编辑  收藏  举报