views:

114

answers:

4

Hi, I would like to create a webpage with browser specific in javascript.

For example:

can I use code like this in my coding part

chrome.tab.onRemoved.addListener() in my webpage.

If it is possible please suggest me.

+2  A: 

No, you cannot access extension-specific APIs from webpages.

David Dorward
+2  A: 

The Navigator object contains all information about the visitor's browser.

http://www.w3schools.com/js/js_browser.asp

I think this is pretty much the extent of what is possible in terms of interacting with the specific browser. You can't access other tabs (for security reasons) or tell when a tab is closed.

Jaco Pretorius
+2  A: 

What exactly are you tring to acheive?

If you want to know when the tab in which your webpage is loaded is closed, then window.onunload should help you.

If you want to know when another webpage is closed, you cannot do this.

UPDATE:
You said that you want to know when the user closes the browser or tab. This is not possible.

But for your purpose (getting feedback), I think all you need is to differentiate whether the user is navigating to a link in your page, or whether the user is typing another URL(or by clicking a favorite).

I think for your requirement, whether the user closes the browser, or whether he types another URL, is the same - the user is navigating away from your site, and at that time you say you want to collect feedback.

This can be done in javascript.

For all the clicks in your page that might lead to a page refresh (hyperlinks, buttons,...), set a flag.
In window.onunload, check whether this flag is set.
- If it is set, then the user has clicked a link in your page, do nothing.
- If the flag is not set then the user is navigating away, time to collect feedback.

Let me know if this would work.

PS: Note that popups/any distractions during window.unload can be very annoying. I understand that this probably is the requirements give to you. But if possible, try other mechanisms to collect (coluntary) feedback from the user.

Nivas
I only need to know whether the user is closing the page or just navigating. In window.onunload and window.onbeforeunload, the events trigger for all navigation and also close event. I particularly need to find the browser or window close event.
Nagarajan
I dont think it is possible to see whether the user is closing the window or not. But for your purpose, I think it is sufficient to see whether the user is navigating via some link in your page, or whether the user has typed another URL (or clicked on a bookmark) to navigate away from your page. Have updated the answer with a possible solution.
Nivas
That was supposed to be 'voluntary' feedback, btw.
Nivas
+1  A: 

You can use use the onbeforeunload event:

<html>  
  <head>
    <script>

    var exit = 1;

    function handleClose()
    {      
        if (exit)
        {
            alert("Closing");
        }
    }

    </script>
  </head>
  <body onbeforeunload="handleClose()">
    <a href="test.html" onclick="exit=0">Navigate to other page</a>
  </body>
</html>
VasilP
That's for the tab where the script is running, it doesn't tell you if another tab is closed...
Jaco Pretorius