簡體   English   中英

根據 URL 更新頁面標題

[英]Updating Page Title Based on URL

假設example.com<title>example

如何自動將example.com/#city1的頁面標題更新為example.com/#city1 example city1 ,將example.com/#city2的頁面標題自動更新為example.com/#city1 example city2等等。

我只有一個index.html文件,我不想將php文件添加到我的 html 文件中,所以請幫我寫一個簡單干凈的代碼。

如果您需要動態解決方案,則每次錨點/哈希更改時都會更新頁面標題:

$(function(){
  // event
  $(window).on('hashchange', function() {
    let hash = location.hash.replace( /^#/, '' );
    document.title = 'example ' + hash;
  });

  // just to set on page load (you might need to tweak in case there is no hash)
  $(window).trigger('hashchange');
});

您必須至少使用 javascript。

嘗試這個:

 var str = 'example.com/#city1'; var str1 = str.substring(0,str.indexOf('.'))+' '+str.substring((0,str.indexOf('#')+1)); document.getElementsByTagName('p')[0].innerHTML = str1; //in your case replace p with title tag
 <p>Change Me</p>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM