Route Changing frequently solution

一.js替換方法:javascript

路徑問題很頭疼, 本方法旨在: 不更改Apache環境前提下, 用newscript.js技巧, 完美替換掉oldscript.js, 核心代碼有點複雜, 不過, 根據自身項目需求, 只需修改如下代碼中'the method can be deleted completely from here:'中間部分.css

改寫的方法參考自: 這裏html

<!--old route:../../static/js/order.js-->
<!--new route can be:/static/js/order.js-->
<script type="text/javascript" src="../../static/js/order.js" ></script>
<script>
    function createjscssfile(filename, filetype){
        if (filetype=="js"){ //if filename is a external JavaScript file
            var fileref=document.createElement('script')
            fileref.setAttribute("type","text/javascript")
            fileref.setAttribute("src", filename)
        }
        else if (filetype=="css"){ //if filename is an external CSS file
            var fileref=document.createElement("link")
            fileref.setAttribute("rel", "stylesheet")
            fileref.setAttribute("type", "text/css")
            fileref.setAttribute("href", filename)
        }
        return fileref
    }

    function replacejscssfile(oldfilename, newfilename, filetype){
        var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none";
        var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none";
        var allsuspects=document.getElementsByTagName(targetelement);
        for (var i=allsuspects.length; i>=0; i--){

            if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
               var b=allsuspects[i].getAttribute(targetattr);
                var x=allsuspects[i].getAttribute(targetattr).indexOf(oldfilename);
               //the method can be deleted completely from here: begin
                console.log(x);
                var c=b.substring(x);
                console.log(c);
                //c爲新的文件路徑, 可根據本身項目路徑, 操做字符串,僅做爲參考
                newfilename=c;
               // the method can be deleted completely from here: end
                var newelement=createjscssfile(newfilename, filetype);
                allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i]);
            }
        }
    }
    //用 "newscript.js" 替換 "oldscript.js"
        replacejscssfile("/static/js/", "newscript.js", "js");

    //用 "newscript.css" 替換 "oldscript.css"
//    replacejscssfile("oldstyle.css", "newscript.css","css");
</script>
js solution

 

二. Apache環境更改java

此方法對開發來講一勞永逸, 不需更改任何js文件或者用到相關知識.ide

弊端是每次開發新項目, 都須要更改Apache環境.(這也意味着每次只能'高效率'作一個項目, 若是開發路徑不一致)spa

過程書寫ing. 算了配置太複雜, 不寫了..(若是讓你鬱悶了請郵件告訴我, yuanqingxiao@outlook.com)code

相關文章
相關標籤/搜索