I'm developing an e-commerce website. When the user changes the tab and goes to another website. I want to change the page title and accordingly tab title too. For example, a user is on the main page and Title look like "Facebook" when a user goes to another page at same browser title should look something like "We miss you". Which javascript method can i use for it? Thank you!
Asked
Active
Viewed 5,796 times
-2
-
1possible duplicate of https://stackoverflow.com/questions/413439/how-to-dynamically-change-a-web-pages-title – Abderrahmane TAHRI JOUTI Dec 22 '17 at 12:34
-
so you mean that when the user navigates out of the current page, the tab title changes? – Umar Waliyullah Dec 22 '17 at 12:43
-
Yes That's right Umar, I meant that. – Mert Şen Dec 22 '17 at 12:50
-
But the user would be on the same domain (your website)? – putvande Dec 22 '17 at 12:53
-
Possible duplicate of [How to dynamically change a web page's title?](https://stackoverflow.com/questions/413439/how-to-dynamically-change-a-web-pages-title) – Al-Mothafar Dec 22 '17 at 13:12
1 Answers
0
Run this snippet, and then click in and out of the results pane. For those who insist on plain JS, now without any $.
window.onblur = function() {
//document.title = 'We Miss You';
console.log('this works');
}
window.onfocus = function() {
//document.title = 'We Miss You';
console.log('this also works');
}
You can use a blur
event:
$(window).blur(function() {
document.title = 'We Miss You';
})
Then, when the user returns, use:
$(window).focus(function() {
document.title = 'Facebook';
})
...to restore the original title

sideroxylon
- 4,338
- 1
- 22
- 40
-
11: This doesn't work, and 2: OP didn't ask for jQuery.. so better would be to answer with plain JS. – putvande Dec 22 '17 at 12:54
-
-
@sideroxylon actually you have to stick with the OP question, otherwise, it is not an answer for the question, also no need to download library just to the simple job that can be done in plain JS, so you might need to ask in comments to get clarification before the answer. – Al-Mothafar Dec 22 '17 at 13:12
-
2The OP asked for a method. The method is `blur` - that can be implemented via jQuery or plain JS. I'm making no assumptions about what else is happening on the page - simply demonstrating that the `blur` and `focus` events can achieve the required outcome. It's up to the OP (or anyone else) to decide how to implement those event. – sideroxylon Dec 22 '17 at 13:16