DeA
DeA

Reputation: 1918

window.open not working properly on Firefox 55.0.1

I'm trying to add Desktop Notifications to my site using the W3C Notifications API, documented at MDN. For an initial test and playing around, I have been using this fiddle I got from this question on Stackoverflow. All works out fine, except when the notification shows up and I click on it while using Firefox, the landing URL opens in a new tab and immediately Firefox switches to the page from which the notification was triggered.

This weird behavior was not seen on Chrome. It works just fine there. What is causing this behavior? Am I doing something wrong?

Here's the code from that fiddle:

<button onclick="notifyMe()">Notify me!</button>


<script>
    // request permission on page load
    document.addEventListener('DOMContentLoaded', function () {
      if (Notification.permission !== "granted")
        Notification.requestPermission();
    });

    function notifyMe() {
      if (!Notification) {
        alert('Desktop notifications not available in your browser. Try Chromium.'); 
        return;
      }

      if (Notification.permission !== "granted")
        Notification.requestPermission();
      else {
        var notification = new Notification('Notification title', {
          icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
          body: "Hey there! You've been notified!",
        });

        notification.onclick = function () {
          window.open("http://stackoverflow.com/a/13328397/1269037");      
        };

      }

    }
</script>

Upvotes: 1

Views: 567

Answers (1)

Julian
Julian

Reputation: 21

Replace:

window.open("http://stackoverflow.com/a/13328397/1269037");

with:

var myWindow = window.open("http://stackoverflow.com/a/13328397/1269037");
myWindow.focus();

Upvotes: 1

Related Questions