1

I have a problem about changing website title during two process.

When I enter webpage, its title has changed via the code shown below.

<script type="text/javascript">

    $(document).ready(function() {
        document.title = 'New Title';
    });

</script>

The sceranio,

  • There are two blank page in my browser. a.html and b.html
  • My website is a.html and its title "A page"
  • When I enter to the a.html, its name changed as New title
  • When I click to the b.html page, I want to change title of a.html as "A Page"

When I am at the any blank page in browser, I want to change its title again. How can I do it?

I found a code snippet about it . Is it right?

<script>

window.onblur = function () { document.title = 'A Page?'; }

window.onfocus = function () { document.title = 'New Title'; }
</script>
S.N
  • 2,157
  • 3
  • 29
  • 78
  • 1
    From your website, you do not have access to an blank page in the browser as there are no script running there. – empiric May 25 '20 at 07:21
  • @empiric I saw a an example about it. – S.N May 25 '20 at 07:23
  • Then please include the example in here and describe why it is not working for you. – empiric May 25 '20 at 07:25
  • @empiric I edit my post. – S.N May 25 '20 at 07:26
  • Ah understood now, I thought with blank page you mean a new tab in your browser, but you have access to both pages.So you would need to detect when you are not on the page or rather when you are leaving a page and trigger the title chang. This [question](https://stackoverflow.com/q/23878277/4202224) might help you – empiric May 25 '20 at 07:30
  • Or [this](https://stackoverflow.com/q/7080269/4202224) one – empiric May 25 '20 at 07:30
  • Something along the code snippet you posted should work, see the 2 questions I linked, one of them uses a similiar solution – empiric May 25 '20 at 07:32

1 Answers1

0

Here is my answer

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

$(function () {
    var message = "New Title!";
    var original;

    $(window).focus(function() {
      if(original) {
        document.title = original;
      }
    }).blur(function() {
      var title = $('title').text();
      if(title != message) {
        original = title;
      }
      document.title = message;
    });
  });
S.N
  • 2,157
  • 3
  • 29
  • 78