LANGUAGE/HTML & CSS & JS 2017. 1. 3. 16:55

!markdown


# Context Path (컨텍스트 패스)


### getContextPath()



```js

function getContextPath() { 

  return window.location.pathname.substring(0, window.location.pathname.indexOf("/",2)); 

}

```


OR


```js

function getContextPath(){ 

  var offset=location.href.indexOf(location.host)+location.host.length; 

  var ctxPath=location.href.substring(offset,location.href.indexOf('/',offset+1)); 

  return ctxPath; 

}

```


사실, 정확하게 Context Path를 가져오는 코드는 아니다.


Context Path가 없는 웹일 경우를 분별할 수 없기 때문에, 그 웹에서는 에러가 발생할 것이다.


모든 상황에 적용할 수 없는 코드들이지만, 나름 필요한 상황이 있을 것으로 보인다.




### 참고

How do you get the contextPath from JavaScript, the right way?:

[http://stackoverflow.com/questions/6615641/how-do-you-get-the-contextpath-from-javascript-the-right-way](http://stackoverflow.com/questions/6615641/how-do-you-get-the-contextpath-from-javascript-the-right-way)


자바스크립트에서 ContextPath 구하기:

[http://blog.naver.com/PostView.nhn?blogId=lsv400&logNo=100044880331&redirect=Dlog&widgetTypeCall=true](http://blog.naver.com/PostView.nhn?blogId=lsv400&logNo=100044880331&redirect=Dlog&widgetTypeCall=true)

'LANGUAGE > HTML & CSS & JS' 카테고리의 다른 글

[Bower] is gone. (잘 가~)  (0) 2018.12.13
[CryptoJS] SEED  (0) 2017.11.29
[JavaScript] 현재 가장 높은 수치의 z-index는?  (0) 2016.12.03
[JavaScript] 정리중...  (0) 2016.08.23
[Internet Explorer Test] IE 테스트하기  (2) 2016.08.21