tags:

views:

48

answers:

3

Hello, i have this:

$counter = $sWall + $sWC + $sOther;
if (!empty($counter)) {
    echo '(' . $counter . ')';
    $counter = 0;
}

And js:

    $(window).blur(function() {
        tracktime = new Date();
         setInterval(function() { 
      $.ajax({ 
       type: "POST",
       url: "imback.php",
    data: {
    mode: 'ajax',
            getUpdates: 'auto',
            },
       success: function(msg){
       document.title = msg + document.title;
    }
     });
}, 2000);

    })

For showing new unread comments in the title bar. Works fine and displays it as (1), although everytime it call, it adds a (1) so the title gets like this: (1)(1)(1)(1)(1)(1)(1)(1).......

And i only want it to do it once (1) no matter how many calls, so if a new call is response is (2) then it will show (2) and not (2)(1)(1)....

How can i fix this the best way?

+3  A: 

When the page is loaded, save the original title to a variable, then when you need to update it, use the saved value.

Maerlyn
A: 

You'll have to store the original title in a variable and then prepend the msg variable to that title variable.

var titleOrig = document.title;

$(window).blur(function() {
    tracktime = new Date();
    setInterval(function() {
        $.ajax({
            type: "POST",
            url: "imback.php",
            data: {
                mode: 'ajax',
                getUpdates: 'auto',
            },
            success: function(msg){
                document.title = msg + titleOrig;
            }
        });
    }, 2000);
})

You should wrap this in an init function somewhere, but it's the general idea.

palswim
+1  A: 
(function () {
    var initial_title = document.title;
    $(window).blur(function() {
        tracktime = new Date();
        setInterval(function() { 
            $.ajax({ 
                type: "POST",
                url: "imback.php",
                data: {
                    mode: 'ajax',
                    getUpdates: 'auto',
                },
               success: function(msg){
                    document.title = msg + initial_title;
               }
           });
        }, 2000);
    });

    $(window).focus(function () { document.title = initial_title; });
})();
Gabi Purcaru
Works although remove the () at last
Johnson
@user457827 the `()` is there with a purpose; I wrapped the script in a self-executing anonymous function, so that it doesn't pollute the global namespace with the `initial_title` variable. You could remove the wrapping function if you wanted, but I put it there because I wanted it to be as "clean" as possible
Gabi Purcaru
I get firebug error on all my other js stuff i have when i have it.
Johnson
hmm, interesting. Check if it works without the wrapper function (just remove the first and last line)
Gabi Purcaru
You probably need to wrap this in an initialization handler.
palswim