run a function before Firefox close the window - javascript

This is my first topic here! So, I'm trying run an ajax function before the Firefox's window close, my code works when I close the tab, but window not. How can I do this?
Detail: Only Firefox don't run the function when window is closed and if possible without use JQuery.
window.onload = function(){
window.addEventListener('beforeunload',function(event){
var ajax = new XMLHttpRequest();
ajax.open("POST","index.php",true);
ajax.send();
}, false);
}

Try navigator.sendBeacon
var some_ajax_data;
window.onbeforeunload = function() {
navigator.sendBeacon('/data.php', some_ajax_data);
};

Related

How to stop window.open from refreshing the window in javascript?

Lets say you have code like this.
function openNewTab(url) {
const newTab = window.open(url, 'windowNameHehe');
};
Now every time you call this function it will open a new tab. However, if the window is already opened it will go to it and refresh/reload it. How do I stop it from reloading/refreshing and just bringing it to view?
Doing something like newTab.focus() or adding the boolean (true/false) in the 4th parameter of window.open isn't working for me either. Any help?
function openNewTab(url) {
var newTab = window.open(url, 'windowNameHehe', , false);
newTab.focus();
}
https://www.w3schools.com/jsref/met_win_open.asp
Open a URL in a new tab (and not a new window) using JavaScript
function openNewTab(url) {
const newTab = window.open('', 'windowNameHehe');
newTab.focus();
if (!newTab.location.hostname) {
newTab.location.href = url;
}
};

What are the attributes of a new window, if opened by a window.open function?

If I add this code between a <script> and a </script>:
function test()
{
var newWindow = window.open("https://stackoverflow.com/", "a", "width = 600,height = 400");
document.write(newWindow.location.href);
document.write(newWindow.innerWidth);
}
test();
And the output is "about:blank" and 0, though I think it should be "https://stackoverflow.com/" and 600.
I am so confused about this and waiting for a explanation.
AND: if I do want to get the URL of the new window, how should I do?
MANY THANKS.
You can capture these values asynchronously if you wait on the load event:
var newWindow = window.open('http://www.stackoverflow.com', 'a', 'width=600,height=400')
newWindow.addEventListener('load', function () {
console.log(newWindow.location.href);
console.log(newWindow.innerWidth);
});
However, as James Thorpe points out, this will only work if the new window is in the same domain as the page where the script is running, and this happens asynchronously, so whatever you're trying to accomplish by using document.write() might not work.

logout when window closed using javascript

I have used window.onbeforeunload on my javascript codes, but apparently it only works for FireFox:
<script type="text/javascript">
window.onbeforeunload = function (e) {
location.href="admin.jsp?action=logout";
};
</script>
I need this to close at least 5 renowned browsers (firefox,IE,opera,safari,and chrome). Can anyone help me out?
Use a synchronous XHR to do this, and use the unload event instead of onbeforeunload:
window.addEventListener('unload', function() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'admin.jsp?action=logout', false);
xhr.send(null);
}, false);

XMLHttpRequest from Firefox Extension

I'm using XMLHttpRequest to exchange data between server and Firefox extension I'm developing. Unfortunately, those requests seem somehow connected with the currently open page - if I try to issue request while the current tab is closing, it will fail with an error. How can I make my requests originate from the extension itself, independently of what's going on in tabs?
EDIT: Here is the code that reproduces this problem. It's run as the main extension body (I based my design on the "Hello world" tutorial from http://kb.mozillazine.org/Getting_started_with_extension_development, so no Add-on SDK). This means that it's executed in the same place as the code from "overlay.js" in above tutorial.
function createXMLHttpRequest() {
return Components.classes["#mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
}
function issueRequest() {
var req = createXMLHttpRequest();
req.open("GET", "http://google.com", true);
req.addEventListener("load", function(event) {
alert("SUCCES");
});
req.addEventListener("error", function(event) {
alert("ERROR");
});
req.send();
};
window.addEventListener("DOMContentLoaded", function(event) {
issueRequest();
var doc = event.originalTarget;
var win = doc.defaultView;
win.addEventListener("unload", function(event) {
issueRequest();
});
});
This results in "SUCCESS" after opening of new tab, and "ERROR" after closing it. I would prefer to have two SUCCESSES.
If that script is running in a browser window overlay then you attached your DOMContentLoaded handler to the wrong node - you will only get notified when the browser window itself loads. Consequently, your unload handler waits for the browser window to the closed, you probably intended to wait for a tab to be closed. The correct code would look like this:
// Wait for the browser window to load before doing anything
window.addEventListener("load", function() {
// Attach a listener to the tabbrowser to get notified about new pages
window.gBrowser.addEventListener("DOMContentLoaded", function(event) {
issueRequest();
var doc = event.originalTarget;
var win = doc.defaultView;
win.addEventListener("unload", function(event) {
issueRequest();
});
}, false);
}, false)

Opening my page in firefox after installing the addon

HI,
My am trying to open my home page after the firefox restarts for the first time after installation.
For this i am adding the event handler on load page and checks where this event is executed for the first time or not.
window.addEventListener("load", initializeOverlay, false);
But my problem is how to open the page in new tab when the firefox get started. I use
`window.open("https://www.xyz.com/");`
but that opens the page in new window that might even be open in internet explorer.
So is there any way to open the page in new tab in same window which is going to be open.
Thanks
BHAVIK GOYAL
I manage to do something similar using preferences, rather than creating files, etc.
Inside of /defaults/preferences/default.js:
pref("extensions.extension_name.just_installed", true);
pref("extensions.extension_name.post_install_url", "http://www.google.com");
Then inside the main JS file for the add-on:
// Retrieve the preferences.
var prefs;
prefs = Components.classes["#mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.extension_name.");
prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
// If we just installed, open the post-install page and update the preferences.
var just_installed = prefs.getBoolPref("just_installed");
var post_install_url = prefs.getCharPref("post_install_url");
if (just_installed) {
prefs.setBoolPref("just_installed", false);
gBrowser.selectedTab = gBrowser.addTab(prefs.getCharPref("post_install_url"));
}
Only problem is Firefox doesn't reset the preferences saved by an extension after that extension is uninstalled.
I got the answer. We can add the gbrowser.open("http://www.xyz.com/") to open in new tab and this statement has to be executed in new function that is by calling the other event handler function loadedoverlay which is defined as follow:
function loadedOverlay() {
try{
var file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(Components.classes["#mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile).path+"\\initialstart.txt");
if ( file.exists() == true )
{
}
else
{
file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
var Website="http://www.emailstationery.com/";
gBrowser.addTab(Website);//This is for new Tab
}
} catch(e) {}
}
The call to this function has to be add in the load event function by adding the code of lines as below:-
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", loadedOverlay, true);

Categories

Resources