Facebook and Yahoo Chat javascript notifications

Hi,

I've noticed on both Facebook and Yahoo chat both play a sound in the browser and alternate the page title / info text in the browser tab when you are on another tab...

I've had a dig around in the pages but I can't see how they are doing this...
Cheers
Craig
craig32768Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StealthyDevCommented:
document.onfocusin and document.onfocusout are used in IE
window.onfocus and window.onblur are used in other browsers.

please find the attached sample for the same...
<script>

function getTime()
{
	var currentTime = new Date()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	var seconds = currentTime.getSeconds()

	var suffix = "AM";
	if (hours >= 12) {
	suffix = "PM";
	hours = hours - 12;
	}
	if (hours == 0) {
	hours = 12;
	}

	if (minutes < 10)
	minutes = "0" + minutes
	return hours + ":" + minutes + ":" + seconds + " " + suffix;
}
function onBlur() {
	//document.body.className = 'blurred';
	document.getElementById("myinput").value += "\nBlurred at " + getTime();
};
function onFocus(){
	//document.body.className = 'focused';
	document.getElementById("myinput").value += "\nFocused at " + getTime()
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
	document.onfocusin = onFocus;
	document.onfocusout = onBlur;
} else {
	window.onfocus = onFocus;
	window.onblur = onBlur;
}
</script>
<textarea id="myinput" cols=50 rows=25></textarea>

Open in new window

craig32768Author Commented:
Hi senthurpandian,

Thanks for the focus demo - So I can detect when the page is blurred. What I don't quite grasp is what to animate / toggle the page title every sec in the tab bar.... I suppose that will be some variant of setInterval I can take a look at.. Do you think that would be a sensible route?

craig32768Author Commented:
Mmmm. I guess what I'm after is a function that loops between two page titles that stops when the document is focused again...
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

StealthyDevCommented:
Here we go !!!
<head><title>org. title</title></head>
<script>
var focusInMe = true;
function getTime()
{
	var currentTime = new Date()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	var seconds = currentTime.getSeconds()

	var suffix = "AM";
	if (hours >= 12) {
	suffix = "PM";
	hours = hours - 12;
	}
	if (hours == 0) {
	hours = 12;
	}

	if (minutes < 10)
	minutes = "0" + minutes
	return hours + ":" + minutes + ":" + seconds + " " + suffix;
}
function onBlur() {
	focusInMe = false;
	//document.body.className = 'blurred';
	document.getElementById("myinput").value += "\nBlurred at " + getTime();
};
function onFocus(){
	focusInMe = true;
	//document.body.className = 'focused';
	document.getElementById("myinput").value += "\nFocused at " + getTime()
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
	document.onfocusin = onFocus;
	document.onfocusout = onBlur;
} else {
	window.onfocus = onFocus;
	window.onblur = onBlur;
}
var orgTitle = document.title;
var myTitle = "test message";
var isShown = false;
function changeTitle()
{
	//alert(!focusInMe && !isShown);
	if(!focusInMe && !isShown)
	{
		document.title = myTitle;
		isShown = true;
	}
	else
	{
		document.title = orgTitle;
		isShown = false;
	}
}
xyz = setInterval("changeTitle()", 2000);
</script>
<input onkeypress="myTitle = this.value;" value="test message"><br>
<textarea id="myinput" cols=50 rows=25></textarea>

Open in new window

StealthyDevCommented:
The above will do what you are expecting..

You can add beep sound with someother flag... :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
craig32768Author Commented:
Thank you - what a start, I was struggling with my basic javascript skills!. Cheers
StealthyDevCommented:
All the best - keep on developing your skills.

Best Regards.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.