I have a static html website. Our server does not support php/c# etc…
Can JS / JQuery / Ajax or others do the following:
If the url is:
Https://example.com/page , the meta title will be e.g. “home page”.
Https://example.com/example#1 , the meta title will be to “new meta title”
Https://example.com/example#29 , the meta title will be e.g. “a different title”
Effectively , can the meta title be dynamic and displays different text based on what the url #identifier is.
Advertisement
Answer
Something like this should do it:
JavaScript
x
14
14
1
<script>
2
function setTitleBasedOnUrlHash() {
3
var hash = window.location.hash;
4
if (hash === '#1') {
5
document.title = 'new meta title';
6
} else if (hash === '#2') {
7
document.title = 'a different title';
8
}
9
// ...more else ifs here...
10
}
11
12
window.onload = setTitleBasedOnUrlHash;
13
</script>
14