萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 取得與修改網頁標題:document.title 操作與分割重組技巧

基本操作


// 取得標題
const title = document.title; // "頁面標題 - 網站名"

// 設定標題
document.title = "新標題";

進階:依分隔符重組標題


/**
 * 依分隔符分割標題,替換指定段落或附加新段
 * @param {string} splitChr 分隔符
 * @param {string} newPart 新段落內容
 * @returns {string} 重組後標題
 */
function recombineTitle(splitChr, newPart) {
  const parts = document.title.split(splitChr);
  if (parts.length > 1) {
    // 替換第二段(索引 1)
    parts[1] = newPart;
    return parts.join(splitChr);
  } else if (newPart) {
    // 無分隔符,直接附加
    return document.title + splitChr + newPart;
  }
  return document.title;
}

// 使用
console.log(recombineTitle("_", "新段落")); 
// 原標題 "A_B" → "A_新段落"
// 原標題 "A" → "A_新段落"

實戰場景:單頁應用 (SPA) 動態標題


// Vue/React Router 導航守衛中
router.afterEach((to) => {
  const baseTitle = "我的網站";
  const pageTitle = to.meta.title || "";
  document.title = pageTitle ? `${pageTitle} - ${baseTitle}` : baseTitle;
});

動態標題提醒(未讀訊息、頁面隱藏)


let originalTitle = document.title;
let blinkTimer = null;

function setNotifyTitle(count) {
  clearInterval(blinkTimer);
  if (count <= 0) {
    document.title = originalTitle;
    return;
  }
  let show = true;
  blinkTimer = setInterval(() => {
    document.title = show ? `(${count}) ${originalTitle}` : originalTitle;
    show = !show;
  }, 1000);
}

// 頁面可見性變更恢復
document.addEventListener("visibilitychange", () => {
  if (!document.hidden) {
    clearInterval(blinkTimer);
    document.title = originalTitle;
  }
});

SEO 與社群分享考量

  • `document.title` 修改**不影響 SEO**,爬蟲讀取初始 HTML ``。</li> <li>社群分享(Open Graph、Twitter Card)需設定 `<meta property="og:title">`,而非依賴 JS 修改。</li> <li>SSR 框架(Next.js, Nuxt)在伺服端渲染時直接輸出正確 `<title>`。</li> </ul> <h2>總結</h2> <ul> <li>基本讀寫:`document.title`</li> <li>結構化重組:`split` + 陣列操作 + `join`</li> <li>SPA 路由同步:導航守衛更新</li> <li>通知提醒:`visibilitychange` + 閃爍定時器</li> <li>SEO/分享:靜態 `<title>` + `<meta og:title>`</li> </ul> <div class="ad2"> <script language='javascript' src='/ad/tad.js'></script> </div> </div> <div> <div class="pageNum"> <ul> <li>上一篇程序編程: <a class='LinkPrevArticle' href='https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/70998.html' >JavaScript 完美 Div 拖曳元件:可配置邊界限制、軸向鎖定、回調事件的完整實作</a> <li>下一篇程序編程: <a class='LinkNextArticle' href='https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/71000.html' >jQuery Deferred 物件完整指南:Promise 模式、鏈式呼叫、多非同步協調與自訂 Deferred 實作</a> </ul> </div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> <div class=hebxbne_left-div-2> <h2 style="text-align:left">JavaScript排行</h2> <ul> <li><a class="font7 nleft" title="JavaScript 禁止輸入中文字符(僅允許英數)" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/70115.html">JavaScript 禁止輸入中文字符(僅允許英數)</a></li> <li><a class="font7 nleft" title="JavaScript 正則限制輸入非法字符(特殊符號過濾)" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/70098.html">JavaScript 正則限制輸入非法字符(特殊符號過濾)</a></li> <li><a class="font7 nleft" title="JavaScript String 物件完整操作指南" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/70589.html">JavaScript String 物件完整操作指南</a></li> <li><a class="font7 nleft" title="JavaScript 前端分頁:Table 與 UL 列表的通用實現方案" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/70679.html">JavaScript 前端分頁:Table 與 UL 列表的通用實現方案</a></li> <li><a class="font7 nleft" title="AngularJS 路由安全實戰:基於角色的客戶端授權攔截器" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/68258.html">AngularJS 路由安全實戰:基於角色的客戶端授權攔截器</a></li> </ul> </div> <div class=kenbxne_js></div> </div> <div class="nright hebxbne_right"> <div class=guanggao1 id=guanggao_r1></div> <div class=kenbxne_js></div> <div class=hebxbne_right-div1> <h2>推薦文章</h2> <div class=kenbxne_js></div> <ul class=hebxbne_right-list1> <li><a class="font7 nleft" title="深入理解 React 框架核心原理:從 MVC 到虛擬 DOM 的演進" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/68045.html">深入理解 React 框架核心原理:從 MVC 到虛擬 DOM 的演進</a></li> <li><a class="font7 nleft" title="JavaScript/jQuery 獲取與設置 label 標簽內容的正確姿勢" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/71059.html">JavaScript/jQuery 獲取與設置 label 標簽內容的正確姿勢</a></li> <li><a class="font7 nleft" title="jQuery outerWidth/outerHeight 詳解:獲取含 mar…" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/69253.html">jQuery outerWidth/outerHeight 詳解:獲取含 mar…</a></li> <li><a class="font7 nleft" title="腳本合併優化:減少 script 標籤提升 JavaScript 性能" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/70666.html">腳本合併優化:減少 script 標籤提升 JavaScript 性能</a></li> <li><a class="font7 nleft" title="JavaScript 透過 className 獲取元素通用函數實作 (getE…" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/71114.html">JavaScript 透過 className 獲取元素通用函數實作 (getE…</a></li> <li><a class="font7 nleft" title="jQuery 判斷 Radio 單選按鈕是否被選中的多種方法" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/68813.html">jQuery 判斷 Radio 單選按鈕是否被選中的多種方法</a></li> <li><a class="font7 nleft" title="JavaScript 千分位格式化與還原:正則替換與 split/join 實現" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/71897.html">JavaScript 千分位格式化與還原:正則替換與 split/join 實現</a></li> <li><a class="font7 nleft" title="JavaScript 身份證號碼驗證完整實作:正則檢核、出生日期合法性、校驗碼算…" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/69601.html">JavaScript 身份證號碼驗證完整實作:正則檢核、出生日期合法性、校驗碼算…</a></li> <li><a class="font7 nleft" title="jQuery 操作 JSON 資料 CRUD 完整實作教學" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/69730.html">jQuery 操作 JSON 資料 CRUD 完整實作教學</a></li> <li><a class="font7 nleft" title="Slider Revolution jQuery 插件:響應式動畫輪播完整配置指…" href="https://twcomputer.wsxdn.com/programming/scripttopics/javascript/201510/68434.html">Slider Revolution jQuery 插件:響應式動畫輪播完整配置指…</a></li> </ul> </div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> <div class=plnebxmj> <div class="plnebxmj-2 flink"> <h2><a class=bottom href="https://twcomputer.wsxdn.com/" target=_blank>萬盛學電腦網</a>| <a class=bottom hreflang="ja" href="https://jp.wsxdn.com/" target=_blank>日本語版</a> | <a class=bottom onclick="this.style.behavior='url(#default#homepage)';this.sethomepage('https://twcomputer.wsxdn.com');" href="#">設為首頁</a> | <a class=bottom href="javascript:window.external.addfavorite('https://twcomputer.wsxdn.com','萬盛學電腦網');">加入收藏</a></h2> </div> <div class=knemnnx></div> </div> <div class="ojwbxnks">copyright © <a title="萬盛學電腦網" href="https://twcomputer.wsxdn.com/" target=_blank>萬盛學電腦網</a> all rights reserved <br> </div> </div> </body> </html>