So I am making a website and everything is nicely done but I don't know that many things with javascript.
I was searching for something that will help me with this and found some similar things but it doesn't work in my case.
This is the problem/idea:
So if someone can help me with the code and explain it a little bit.
Thank you.
You need to make use of the onblur and onfocus events for the window object.
So something like this (this is native javascript, no jquery).
<script>
window.onblur = function () { document.title = 'you went?'; }
window.onfocus = function () { document.title = 'you came back'; }
</script>
$(window).focus(function() {
document.title = 'defult title';
});
$(window).blur(function() {
document.title = 'you went?';
});
You can use :
window.addEventListener('blur',function(){
alert("please Came Back");
document.head.title = "your tilte"
});
window.addEventListener('focus',function(){
alert("Hi");
document.head.title = "your tilte"
});
best for me is use:
window.onload = function() {
var pageTitle = document.title;
var attentionMessage = 'Come Back!';
document.addEventListener('visibilitychange', function(e) {
var isPageActive = !document.hidden;
if(!isPageActive){
document.title = attentionMessage;
}else {
document.title = pageTitle;
}
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.