아래는 jQuery로 현재 메뉴에 대한 내용을 동적으로 title 속성을 부여하는 코드이다.
메뉴 구조 예시 데이터
const menuList = [
{ 'menuName' : 'Login', 'menuPath' : '/login' }
, { 'menuName' : 'Password Recovery', 'menuPath' : '/password' }
, { 'menuName' : 'Create Account', 'menuPath' : '/register' }
];
동적으로 title 속성 부여하는 코드
function gfnSetTitle(menuList) {
var sRealPath = $(location).attr('pathname'); // "/login"
for (var menu of menuList) {
if (menu.menuPath === sRealPath) {
$(document).attr("title", menu.menuName + " | 사이트명");
}
}
}