Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a browser code in javascript

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.

like image 522
Nagarajan Ganesh Avatar asked Sep 17 '10 12:09

Nagarajan Ganesh


People also ask

Can you code a website in JavaScript?

In short, JavaScript is a programming language that lets web developers design interactive sites. Most of the dynamic behavior you'll see on a web page is thanks to JavaScript, which augments a browser's default controls and behaviors.

How do I code JavaScript in browser?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.

Can we add HTML code in JS file?

Using the innerHTML attribute: To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.

How can I create my own browser?

Run Visual Studio and start a new project by going to the File menu and clicking on "New Project." Browse over "Text" and select "Web Browser" in the form page that appears. Go to "View" in the top menu bar, browse over "Other Windows" and click on "Toolbox." This will display the toolbox.


2 Answers

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 given to you. But if possible, try other mechanisms to collect (voluntary) feedback from the user.

like image 81
Nivas Avatar answered Oct 18 '22 14:10

Nivas


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

like image 42
Quentin Avatar answered Oct 18 '22 15:10

Quentin