Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification onclick - Open a link in new tab and focus

function spawnNotification(theBody, theIcon, theTitle, theLink) {
  var options = {
    body: theBody,
    icon: theIcon
  }
  var notification = new Notification(theTitle, options);
  notification.onclick = function() { 
    var myWindow = window.open(theLink,"_blank");
    myWindow.focus();
  };
  setTimeout(notification.close.bind(notification), 4000);
}

I am trying to open a new tab and to focus it , when the notification box is clicked.

I am using the above fuction to open a link in a new tab and to focus on that newly opened tab. But its not working.

New tab is opened, but the focus remains on the old tab itself. How can I solve this?

like image 247
Anoop Mayampilly Muraleedharan Avatar asked Jun 17 '17 09:06

Anoop Mayampilly Muraleedharan


1 Answers

function spawnNotification(theBody, theIcon, theTitle, theLink) {
  var options = {
    body: theBody,
    icon: theIcon
  }
  var notification = new Notification(theTitle, options);
  notification.onclick = function(event) {
    event.preventDefault(); // prevent the browser from focusing the Notification's tab
    window.open(theLink, '_blank');
  }

  setTimeout(notification.close.bind(notification), 7000);
}

I changed my code as shown above. Now it works perfect. See: https://developer.mozilla.org/en-US/docs/Web/API/Notification/onclick#Examples

like image 181
Anoop Mayampilly Muraleedharan Avatar answered Nov 08 '22 02:11

Anoop Mayampilly Muraleedharan