views:

88

answers:

3

Hello, i develop a website and i need let the document title blinking when the browser lost the focus to get the attention from the user.

This is a common task for example in some social network. Btw my javascript code work fine in Chrome, Firefox, Opera, but not in IE7 (that i am testing before release the site)

IE7 have a strange behavior because if i print the document.title in a debug text (you can see in the code), it's changed but the browser still show the previous document title

I try to search a lot on internet to try to fix this problem but with no luck so i decided to post the question in this site. Here my javascript code below and thanks in advance for the suggestions.

the JS method is called by this.blink(true)

// other methods above and below ....

this.blink = function(Action)
{
    if (Action)
    {
        if (!this.blinking)
            this.oldTitle=top.document.title;
        else
            clearInterval(this.blinkTimer);

        // debug current title
        $('debugText').value = 'ORIGINAL ' + top.document.title + '\n' + $('debugHistory').value;

        this.blinkTimer = setInterval(function() {

            var msg='MSG', newTitle

            if (top.document.title == msg)
                newTitle =  '----';
            else
                newTitle =  msg;

            // assign title
        top.document.title =  newTitle;

            // debug blinking, is really changed but not shown <---
            $('debugText').value = 'BLINK ' + top.document.title + '\n' + $('debugHistory').value;

         }, 1000);

    }
    else
    {
        clearInterval(this.blinkTimer);

        if (this.blinking)
            top.document.title = this.oldTitle;
    }

    this.blinking = Action; 
}
A: 

Instead of top.document.title try top.document.getElementsbyTagName('title')[0] (This is assuming top is some form of frame or window)

Alex Nolan
Hi Alex, first of all thanks for your suggestion and I tried this on IE7:document.getElementsbyTagName('title')[0]=newTitle anddocument.getElementsbyTagName('title')[0].text=newTitlein both of cases IE throw an error:"Object don't support this property or method"(i use IE Toolbar debug / Companion JS debugger / DebugBar to try debug IE7)i also tried using prototype.js ($$ is equivalent of getElementsbyTagName)$$('title')[0].text=newTitleIn this case work in all browsers except IE :( no any error coming out but no any effects as before document.title = newTitle
Luka
Look like when IE7 dont have the focus, cant change the title bar, may be is a security issue to prevent phising or something ? I'd check all the options in IE and set low security for test but still not luck.In real i dont like IE 7 but i must support it because still many users are using IE 7.
Luka
Luka, `.text` isn't the property. You want to change `document.getElementsbyTagName('title')[0].childNodes[0].nodeValue`, which is the text node itself.
Eli Grey
@Eli, thanks for your suggestion, i tried with `.text` before because i notice that property by checking with Firebug in Firefox. I tried also your suggestion as `document.getElementsbyTagName('title')[0].childNodes[0].nodeValue=newTitle` but IE7 still throw up the error `Object don't support this property or method`. Seem IE7 work very bad with DOM. Seem very hard to change the title of a document, it's a easy thing for my opinion but not for IE, more suggestion ? i am hopeless :)
Luka
I just noticed that `getElementsbyTagName` was mis-capitalized by the original poster. It's `getElementsByTagName`. If that doesn't help, also try changing `.innerHTML` and `.innerText` of the title node in IE.
Eli Grey
@Eli, thanks for your fix but in all the case still dont work. I wanna remark that `document.title = 'some text'` work fine in IE7 if the browser have the focus or the user interact. The problem is when the browser dont have the focus. As told above, if i inquiry the document.title in a debug text, it's changed, but not display in the title bar. Look like a glitch or a security issue (also if i set low security in IE7).This article can give an example: http://bytes.com/topic/javascript/answers/161441-how-get-page-title (innerText dont change the tile, innerHTML get runtime error)
Luka
A: 

Try this in IE

this.blink = function (Action)
{
     if (Action)
    {

        if (!this.blinking)
            this.oldTitle=top.document.title;
        else
            clearInterval(this.blinkTimer);


        this.blinkTimer = setInterval(function() {

            var msg='MSG', newTitle

            if (top.document.title == msg)
                newTitle =  '----';
            else
                newTitle =  msg;

            // assign title
        top.document.title =  newTitle;


         }, 1000);

    }
    else
    {
        clearInterval(this.blinkTimer);

        if (this.blinking)
            top.document.title = this.oldTitle;
    }

    this.blinking = Action; 
}
  window.blink('now');​​​​

Mostly it will be an issue that window.onblur etc. is not triggering your blink function. If the above works, then you can use mouse movement to track timeout.

Alec Smart
Thanks Alec for your suggestion, actually i prefer to monitor the events about focus as mentioned in my added comments post above, but sure i will try also your suggestion :)
Luka
@Alec, in real the code above that i post is more longer and the event is triggered well because in the debug textarea i see the change and the blinking, if you notice in my code post above, the strange thing is that this row `$('debugText').value = 'BLINK ' + top.document.title` really shown the blink text as expected and come from the title property the but in IE7 title bar is not shown and still shown the normal title without any changes.
Luka
A: 

If you're using jQuery, I've made a plugin called Title Alert for the purpose of blinking notification messages in the browser title bar. With it, you can specify different options like duration, blinking interval, if the blinking should stop when the window/tab gets focused, etc. I've verified that the plugin works in IE6, IE7, IE8, Firefox, Chrome and Safari.

Here is an example on how to use it:

$.titleAlert("New chat message!", {
    requireBlur:true,
    stopOnFocus:true,
    interval:600
});

If you're not using jQuery, you might still want to look at the source code (there are a few quirky bugs and edge cases that you need to handle when doing title blinking if you want to fully support all major browsers).

heyman