---
title: "如何在js文件中如何获取basePath"
author: "runfeel"
date: "2013-07-10T11:43:00Z"
tags: []
slug: "js-basepath"
draft: false
lastmod: "2013-07-12T14:17:32Z"
id: 1904718
url: "https://runfeel.iteye.com/blog/1904718"
---

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';
```