Skip to main content

Changing chrome title tab when users leave

The other day I was on https://fabriceleven.com/ reading one of their blogs and when I tabbed away I saw the title change and I thought it was cool as I had not seen anyone else use it before even though its pretty simple.

So I went about doing it myself. I've done it in pure JS as a lot of the examples are jQuery and for simple things like this you don't need a library.

You can see this working on this page just click on another tab then come back.

Below is the code I used to achieve this. Simply put I get the html title tag. Add an event for when the user is away or using the page with blur and focus on the window. Then I pick a random string to display when they are away.



<!DOCTYPE html> 
<html> <head> 
<title>Hi this is the original title</title> 
</head> 
<body> 
<p> lorem ipsm </p> 
</body> 
<script type="text/javascript"> 
 var title = document.querySelector("title"); 
window.addEventListener("blur", function(event) { 
userAway(); 
}); 

window.addEventListener("focus", function(){ 
userHere(); 
}); 

function userAway() { 
let phrases = [ 
"I miss you", 
"come back soon", 
"Don't forget to read this", 
"You gotta see this" 
]; 

let number = randNumber(); 
number = Math.round(number); 
let currentPhrase = phrases[number]; 
title.innerHTML = currentPhrase; } 
function userHere() { 
title.innerHTML = "Hi this is the original title"; 
} 

function randNumber() { 
return Math.random() * (3-0) + 0; 
} 
</script> 
</html>

A cool thing you could do with this is replace it with the last paragraph title they were reading, a bit like a book mark for a particularly long page. Or a nice message if they keep coming back but this is just a thought and a quick 5 min Sunday funday project.

Thanks for reading.

Comments

Popular posts from this blog

Ionic RSS feed using Feednami

Tutorial of how to make a RSS feed using Feednami which is an alternative to google api which no longer works. This is a basic example and can be improved. First go ahead and make a new ionic project $ npm install -g cordova ionic $ ionic start exampleProject blank Now find index.html and insert... <script src="https://storage.googleapis.com/feednami-static /js/feednami-client-v1.0.1.js"></script> Into the header, then in <ion-content> below Ionic Blank Starter insert a new div <div id ="feed" ng-controller="feedCtrl"> </div> Open app.js, now we are going to make the controller from Feednami that will get our rss feed. .controller('feedCntl', function(){ var url = 'http://daringfireball.net/feeds/articles' feednami.load(url,function(result){ if(result.error){ console.log(result.error) } else{ var entries = result.feed.entries for(var i = 0; i < entri...

Google daydream and all its awesomeness.

Google Daydream  At Google io in 2016,  Google announced: Google daydream. A platform that can bring VR to everyone throughout the use of mobile phones and their new viewer. I was so inspired watching this I felt a need to make Daydream content. However to be able to develop this you need a Google view (£69 from Google store) and a daydream ready phone (£599 and up from Google store). I still plan to do this and will be buying a red viewer very soon (Im so excited XD) and will do an unboxing so look out for that! Then I will be saving up for the pixel phone (which is beautiful) and then onward with making awesomeness. Developing awesomeness  Its not just awesome because of the new viewer and the new pixel phone but the amount of help from google for developers is amazing they already have SDK kits for Android, Unity and Unreal. Unity and Unreal and two powerful game engines both have pros and cons but both are great for making games. I have experie...

BBC micro bit go un-boxing

The BBC micro bit  A while ago I attended Hacksoton  which is a hack day in Southampton, UK. It was a great day and I got to see and hear new and interesting technology (I would recommend for children and developers in the area). One of these technologies was the BBC Micro bit  . What is it for?  Its designed to get children into coding, you can use python, javascript or block editors. I loved them so much that I decided to get myself one as it can also be used with raspberry pi. So below is a video of me opening my micro bit go and I hope to do many exciting projects in the upcoming year and will be writing up about them so keep an eye out! What would you like to see with the micro bit? let me know in the comments below.