JS动态修改网站图标以及标题

const changeFavicon = link => {
	let $favicon = document.querySelector('link[rel="icon"]');
	// If a <link rel="icon"> element already exists,
	// change its href to the given link.
	if ($favicon !== null) {
		$favicon.href = link;
		// Otherwise, create a new element and append it to <head>.
	} else {
		$favicon = document.createElement("link");
		$favicon.rel = "icon";
		$favicon.href = link;
		document.head.appendChild($favicon);
	}
};
let icon = ''; // 图片地址
changeFavicon(icon); // 动态修改网站图标
let title = ''; // 网站标题
document.title = title; // 动态修改网站标题
相关推荐