雷达智富

首页 > 内容 > 网页技术 > 正文

网页技术

动态修改网站标题_动态随机修改网址标题

2022-09-28 216

随机提取某个标题作为标题

<script type="text/javascript">
var title=new Array(5);
title[0]="第一个副标题";
title[1]="下一个副标题";
title[2]="第三个副标题";
title[3]="第四个副标题";
title[4]="最后一个副标题";
var indexTitle=Math.floor(Math.random()*title.length); //获取一个随机的整数
onload=function change(){ //绑定在onload事件当中,只有在加载页面的时候标题才会发生变化
 var curTitle=title[indexTitle]; //获取数组的指定项
 window.document.title+="-"+curTitle;  //更改当前页的标题
 
}
</script>

获取固定作为标题

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; // 动态修改网站标题


赞一波!

文章评论

全部评论