From the picture I assume that you wannawant to have (9) Title
or (99) Title
or (99+)
foras notification indicator.
Here is the clean code that I am using.
Works in vanilla jsVanilla JS, jqueryjQuery, reactjsReactJS, angularjsAngularJS (not tested) and most probably any jsJavaScript framework or library
filterDocumentTitle = () => document.title.replace(/^\((\d+|\d+\+)\)/gm, '').trim()
Usage
if(notification > 0) {
document.title = `(${notification}) ${filterDocumentTitle()}`
} else {
document.title = filterDocumentTitle()
}
Jquery
$.ajax({
url: '/api/notification-check',
method: 'POST',
success: function(notification) {
if(notification > 0) {
document.title = `(${notification}) ${filterDocumentTitle()}`
} else {
document.title = filterDocumentTitle()
}
}
});
Axios
axiso.post('/api/notification-check').then(({ data }) => {
if(data.notification > 0) {
document.title = `(${data.notification}) ${filterDocumentTitle()}`
} else {
document.title = filterDocumentTitle()
}
})
The filterDocumentTitle
function removes everything that matchmatches the above examples as notification indicator.