Javascript : Change page title to get viewer attention
Nowadays, most browser users will open multiple tabs of web pages to view couple of pages by tabbing in and out. However, there are times that you might want to draw the attention of the users back to a page...such as when there are unsaved data or form that needs to be completed.
Below is a simple Javascript trick that you can deploy in your own web pages in order to draw the user's attention back to a web page. What this script does is to detect if the user has navigated to another tab on the browser and will change the page title to "Come Back!". Once the user clicks on the tab again, the script will restore back the original title. Here you go!
<title>This is a random page title</title>
<script type="text/javascript">
var isTabActive;
window.onfocus = function () {
isTabActive = true;
document.title = "This is a random page title";
};
window.onblur = function () {
isTabActive = false;
document.title = "Come Back!";
};
// test
setInterval(function () {
console.log(window.isTabActive ? 'active' : 'inactive');
}, 2000);
</script>
In case you want to test this Javascript out together the Golang, please run the code below and point your browser to localhost:8080
, open a new tab and you will instantly see that the page title change to "Come back!".
package main
import (
"net/http"
)
func Homepage(w http.ResponseWriter, r *http.Request) {
html := `
<title>This is a random page title</title>
<script type="text/javascript">
var isTabActive;
window.onfocus = function () {
isTabActive = true;
document.title = "This is a random page title";
};
window.onblur = function () {
isTabActive = false;
document.title = "Come Back!";
};
// test
setInterval(function () {
console.log(window.isTabActive ? 'active' : 'inactive');
}, 2000);
</script>`
w.Write([]byte(html))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", Homepage)
http.ListenAndServe(":8080", mux)
}
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.4k Swift : Convert (cast) Float to String
+21.9k Golang : How to run Golang application such as web server in the background or as daemon?
+7.6k Golang : get the current working directory of a running program
+8.9k Golang : How to capture return values from goroutines?
+9.7k Golang : Get current, epoch time and display by year, month and day
+10.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+28.4k Get file path of temporary file in Go
+38.9k Golang : How to read CSV file
+15.8k Golang : Read large file with bufio.Scanner cause token too long error
+14.9k Golang : How do I get the local IP (non-loopback) address ?
+9.1k Golang : Generate random Chinese, Japanese, Korean and other runes
+6.3k Golang : Handling image beyond OpenCV video capture boundary