如何在js文件中如何獲取basePath

js路徑的問題有時候不好處理,在jsp中,我們可以用el表達式直接獲取basePath,但是在單獨js文件中不能用el表達式,又不想在jsp中單獨的寫個變量,可以用以下方法:

  

var location = (window.location+'').split('/'); 
var basePath = location[0]+'//'+location[2]+'/'+location[3]; 
var url = basePath + '/js/xxx.js'; 

var location = (window.location+'').split('/');
var basePath = location[0]+'//'+location[2]+'/'+location[3];
var url = basePath + '/js/xxx.js';

 

You May Also Like