How to update page title with notifications? [closed]

3

I am making a script that updates the page title and places how many notifications the user has before the current title. However as it is dynamic and updates this page to check if there are new notifications it is giving problem in the title. Let me give you an example:

I have 01 notification and the current title of the page is Home, so the title is thus "(1) Home", as it verifies if there are new updates the title goes like this: "(1) (1) (1) (1) (1) Home & Pets I wanted some help so this did not happen!

var title = document.getElementsByTagName("title")[0].innerHTML;
document.title = "('.$total_notifi.') "+title; 

This is the script I currently have, where the PHP tag with $total_notifi is the number of user notifications.

    
asked by anonymous 15.01.2016 / 11:46

3 answers

6

The problem is, you're probably picking up the title every time it will update it. This is why (1) (1) is being concatenated ...

You can save the document title to a variable as soon as you run the script and keep it as if it were a constant, only once. Later you replace the current title with the value of the variable you picked up as soon as the script was run, containing the first title.

/*
 * Coloquei um título para exemplificar. No caso, CONST_TITLE pegaria
 * o primeiro valor do próprio título do documento, e.g:
 * 
 *
 *    var CONST_TITLE = document.title;
 *
 */
 
 
var CONST_TITLE = 'Meu título incrível';

function setTitulo(notificacoes) {
  document.title = '(' + notificacoes + ') ' + CONST_TITLE;
}

setTitulo(10);
console.log(document.title); // (10) Meu título incrível

setTitulo(100);
console.log(document.title); // (100) Meu título incrível

console.log(CONST_TITLE);   // Meu título incrível
    
15.01.2016 / 12:19
5

It may be a problem in PHP if you're not using the sum by JavaScript, but if it's on the front end then I'll explain what might be going on.

Assuming the code is actually this ( since what was posted does not make sense ):

<?php
echo 'document.title = "(' . $total_notifi . ') " + title;';

Then the updates now occur in the front-end by increment, so the code here can update the title by accessing the title already coming from php (backend)

Something like:

document.title += "(1)";

If the problem really is in javascript, then you should convert to int using parseInt for example and also do the necessary treatments, something like:

  • javascript:

    //Valor que virá do ajax ou js que você fez e será somado
    function atualiza(soma) {
        var re = /^\(\d+\)/g;
        var res = document.title.match(re);
    
        //Pega o total no titulo
        var atual = res && res[0] ? parseInt(res[0].replace(/\(|\)/g, "")) : 0;
    
        //Remove o total e os '()' do titulo
        var titulo = document.title.replace(re, "");
    
        document.title = "(" + (atual + parseInt(soma)) + ") " + titulo;
    }
    
    setInterval(atualiza, 1000, 1); //Adiciona +1 (terceiro parâmetro)
    
  • html:

    <title>(<?php echo $total; ?>) Home</title>
    

    However as I already mentioned, the problem may be in php.

Note that (<?php echo $total; ?>) is optional and if the update comes from ajax, as it is on Facebook or Twitter, that is, the script here can adapt to any title without having to be in a variable, examples of pages that the (1) will be added:

home:

<title>Home</title>

Blog:

<title>Blog</title>

And Ajax (Jquery example) would look something like:

$.ajax("update.php").done(function(data) {
     atualiza(data);
});

After running it on any page the result will be something like:

home:

(2) Home

Blog:

(2) Blog

That is this way that made the title is updated regardless of the original content.

    
15.01.2016 / 12:31
3

Hello, you can withdraw your previous notification before putting the new one as follows.

var title = document.getElementsByTagName("title")[0].innerHTML;
title = title.substring(title.indexOf(")"), title.length);
document.title = "('.$total_notifi.') "+title; 
alert(title);
    
15.01.2016 / 12:18