[JavaScript, jQuery] 현재 URL과 경로을 가져오는 방법

현재의 URLa태그의 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 등을 가져올 수 있다.

출처: [jQuery] 현재 URL, 경로 가져오는 방법 - shaking blog