2015-07-20 49 views
-1

我有一個main.less文件,其中我有2個減檔,我想基於URL爲加載的2,對於如:進口.LESS文件動態基於URL

if url contain value=abc 
@import 'test1.less'; 
if url contain value=xyz 
@import 'test2.less'; 

我使用的只有JavaScript,沒有JQuery。

在此先感謝。

回答

0

少即是預編譯您的建議在運行時運行一些代碼來改變預編譯的代碼,除非你使用它的客戶端,其在生產環境中會導致性能下降和潛在的可靠性問題,請參閱http://lesscss.org/#client-side-usage

我建議這個心不是可能您使用編譯CSS文件的東西,如:

var = cssURL; 
if(window.location.href =="www.someurl.com/home"){ 
    cssURL = "style1.css"; 
}else{ 
    cssURL = "style2.css"; 
} 
var head = document.head, link = document.createElement('link') 

link.type = 'text/css'; 
link.rel = 'stylesheet'; 
link.href = cssURL; 

head.appendChild(link); 

但是,如果你使用較少的客戶端只需要改變

link.rel = 'stylesheet/less'; 
+0

謝謝你,但我必須與.less一起工作 – Samit

+0

你是不是預編譯你的Less?如果您在生產環境中使用更少的文件客戶端,這是不好的做法,請參閱http://lesscss.org/#client-side-usage – sjm

0
Thanks for ur help....I found a rather much better way...add a class to body tag and make these entries in main less file 
.class1 { 
    @import 'test1.less'; 
} 
.class2 { 
    @import 'test2.less';} 

and then identify ur url and change class name 

document.getElementsByTagName("body")[0].className="class2" 

thats it