현재의 URL
과 a
태그의 href
가 동일할 때, a
태그에 .addClass('on')
을 하기 위해서 경로를 가져오는 방법을 확인해보았다.
JavaScript의 경우
window.location.host // returns host
window.location.hostname // returns hostname
window.location.pathname // returns pathname
window.location.href // returns full current url
window.location.port // returns the port
window.location.protocol // returns the protocol
window.location.origin
jQuery를 사용할 경우
$(location).attr('host'); // returns host
$(location).attr('hostname'); // returns hostname
$(location).attr('pathname'); // returns pathname
$(location).attr('href'); // returns href
$(location).attr('port'); // returns port
$(location).attr('protocol'); // returns protocol
$(location).attr('origin');
- 현재 URL, 경로 뿐 아니라 선택자로
a
태그를 선택하여href
등을 가져올 수 있다.