[英]Updating Page Title Based on URL
Let's say the <title>
of example.com
is example
假设
example.com
的<title>
是example
How to automatically update the page title for example.com/#city1
to example city1
and example.com/#city2
to example city2
and so on.如何自动将
example.com/#city1
的页面标题更新为example.com/#city1
example city1
,将example.com/#city2
的页面标题自动更新为example.com/#city1
example city2
等等。
I have only an index.html
file and I don't want to add php
file to my html file so please help me with a simple and clean code.我只有一个
index.html
文件,我不想将php
文件添加到我的 html 文件中,所以请帮我写一个简单干净的代码。
If you want a dynamic solution, that will update the page title everytime the anchor/hash changes:如果您需要动态解决方案,则每次锚点/哈希更改时都会更新页面标题:
$(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');
});
You have to use atleast javascript.您必须至少使用 javascript。
Try this:尝试这个:
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.