카테고리 없음
Javascript | 자바스크립트에서 contextPath 구하기
zuyo
2017. 7. 19. 15:34
반응형
프로젝트하실 때 보통 Java나 php같은 프로그래밍 언어로 contextPath를 구해오는데요.
url에서 자바스크립트로 간단하게 contextPath를 구하는 함수를 만들어 보겠습니다.
function getContextPath() {
var hostIndex = location.href.indexOf( location.host ) + location.host.length;
return location.href.substring( hostIndex, location.href.indexOf('/', hostIndex + 1) );
};
http://localhost:8080/test/code/login
URL 주소가 위와 같다면 /test 라는 contextPath를 구하게 됩니다.
만약, 네이버 블로그를 가입해서 운영하신다면
http://blog.naver.com/zuyo
이런식으로 자신의 가입아이디가 contextPath가 됩니다.
예
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function getContextPath() {
var hostIndex = location.href.indexOf( location.host ) + location.host.length;
return location.href.substring( hostIndex, location.href.indexOf('/', hostIndex + 1) );
};
$(document).ready(function(){
$("#viewText").html( getContextPath() );
});
</script>
</head>
<body>
</body>
</html>
반응형