-2

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!

Mert Şen
  • 1
  • 1

1 Answers1

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
  • 3,977
  • 1
  • 18
  • 33
  • 1
    1: 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
  • I suggest you try it. – sideroxylon Dec 22 '17 at 13:06
  • @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
  • 1
    The 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