[js] 특정 주소에서 html 에 class 추가하기

JavaScript
if ( window.location.pathname == '/' ) {
	var root = document.documentElement;
	root.className += ' index';
}

wondow.location.pathname에 지정된 경로의 URL로 접속 할 경우 index라는 class를 html에 추가해준다.
즉, 현재 보고있는 주소가 http://wallel.com/ 일 경우 html class=" index"가 된다.

만약 http://wallel.com/188 에서 사용하려면 window.location.pathname == '/188' 이라고 하면 된다.

index 앞의 스페이스바를 제거할 경우 html class="test index"이렇게 나와야 하는게 html class="textindex" 이렇게 나오니 주의하자.

참고자료

  1. http://www.cmsfactory.net/node/10554
  2. http://stackoverflow.com/questions/13980982/add-class-to-html-with-javascript
addClassjavscript
블로그
프로젝트
스터디