다음 함수는 즐겨 찾기 아이콘을 url로 변경합니다.
https://htmldom.dev/change-the-website-favicon
const setFavicon = function(url) {
// Find the current favicon element
const favicon = document.querySelector('link[rel="icon"]');
if (favicon) {
// Update the new link
favicon.href = url;
} else {
// Create new `link`
const link = document.createElement('link');
link.rel = 'icon';
link.href = url;
// Append to the `head` element
document.head.appendChild(link);
}
};
즐겨 찾기 아이콘을 소셜 웹 사이트의 사용자 아이콘과 같이 동적으로 업데이트 하려는 경우 :
setFavicon('/path/to/user/profile/icon.ico');
이모티콘을 즐겨 찾기 아이콘으로 사용
위의 setFavicon() 함수는 파비콘의 URL을 허용합니다. 맞춤 URL을 전달하여 멋진 것을 얻을 수 있습니다.
아래 코드에서 캔버스 요소를 만들고 특정 이모티콘으로 채우고 맞춤 URL을 가져옵니다.
const emojiFavicon = function(emoji) {
// Create a canvas element
const canvas = document.createElement('canvas');
canvas.height = 64;
canvas.width = 64;
// Get the canvas context
const context = canvas.getContext('2d');
context.font = '64px serif';
context.fillText(emoji, 0, 64);
// Get the custom URL
const url = canvas.toDataURL();
// Update the favicon
setFavicon(url);
};
// Usage
emojiFavicon('?');
등록된 댓글이 없습니다.