Scripts may close only the windows that were opened by them - javascript

I have tried many ways like
var win = window.open("","_self");*/
win.close();
and like this
window.opener=null;
window.open('','_self');
window.close();
window.history.go(-1);
document.body.hide();
But not working.

Maybe you can try this, it works for me.
try {
window.opener = window;
var win = window.open("","_self");
win.close();
top.close();
} catch (e) {
}

The script runs inside window.prototype, imported by the respective call inside the html markup. Unless you import the script to gain access to the respective window, like using a library, i don't think you can do it.

Related

How to run a method when the window closes in JavaScript

I'm wondering how I can run a method when a window or tab closes. I've tried using the following...
window.onclose = () => {
// execute function
}
window.onbeforeunload = () => {
// execute function
}
but both of these only fire when I refresh the page not when I close it.
Any help would be very appreciated thanks!
Not possible anymore.
Newer browsers doesn't support it anymore.

.onbeforeunload sometimes returns, sometimes doesn't (popup,IE11)

I'm working on this code at work. Basically, it returns a console.log whenever the popup is closed. Using .onbeforeunload works with every browser but IE11. With IE11 it'll work sometimes and sometimes it doesn't call at all. I'm not doing anything different, just opening the popup then closing it. I've no idea how to fix this as it's temperamental. I've pasted the function code below. Thanks
function open(config, refID, cb) {
var w = window.open(
config.baseURL,
"_blank",
"toolbar,scrollbars,resizable,top=250,left=250,width=599,height=500"
);
w.onbeforeunload = function () {
if (cb) cb()
};
}
I really don't know how you managed to make your code work. It simply won't. The problem is that you can't add an event like you are trying. You need to use addEventListener. I made a simple snippet. Works 100% of times. No temperamental code, if such thing even existed :)
function open(url, callback) {
// Open the window
var win = window.open(url, "popup", "toolbar=1, scrollbars=1, resizable=1, top=250, left=250, width=500, height=500");
// Add the event listener
win.addEventListener('beforeunload', function () {
if(callback) callback();
}, false);
}
// Opens the window, with the url and callback as arguments
open('whatever.html', function() {
console.log('hello!');
});
Remember that you need permissions on the opened window url to be able the add the event listener to it.
Hope it helps.

closing javascript child window

function displaymessage()
{
window.open("http://www.sabah.com.tr","_blank")
parentWindow.close
}
How should I write instead of "parentWindow.close" to close the child page?
not 100% sure what you mean, maybe you want this:
function displaymessage() {
var win = window.open("http://www.sabah.com.tr","_blank")
win.close();
}
You need to maintain a reference to the opened window:
function displaymessage() {
var myWin = window.open("http://www.sabah.com.tr","_blank");
myWin.close();
}
..but most browsers will prompt you saying "this page is trying to close....", just be aware of that, and test various browsers to make sure you get the desired result.

How to find out if DOMContentLoaded event comes from iframe or not?

I am working on a Firefox extension and it uses something like this:
function myExt()
{
this.handleEvent = function (event)
{
switch (event.type)
{
case "DOMContentLoaded":
{
alert('fired');
}
}
}
window.addEventListener ("DOMContentLoaded", this, false);
}
My problem is that the alert gets executed multiple times if the page contents iframes, so what I am looking to do is, using "event" on this.handleEvent I need to find out if event.target references the top window or the iframe window.
How can I do this?
You could try checking for event.target.frameElement. If it's undefined then it's the main document, but if it is defined then it's a frame. I can't remember if this is only for frames, though, or if it's for iframes too.
Yeah, iframe is weird in that will fire DOMContentLoaded. It treats the content as if it were in its own 'window'. Have you tried getting the event.target's parent or ownerDocument?
I used Firebug on an SVG loaded into an <object> tag and I believe that ownerDocument gets what you're looking for.
I am still doing some tests but it looks like this function does the job:
this.isTopLevel = function(event){
var doc = event.target;
for(var i = 0; i < gBrowser.browsers.length; i++) {
if(gBrowser.browsers[i].contentDocument == doc) return true;
}
return false;
};
I'm using this snippet to filter out iframes:
var browser = gBrowser.getBrowserForDocument(event.target);
var pageIsFrame = (event.target instanceof Ci.nsIDOMHTMLDocument &&
event.target != browser.contentDocument);
if (pageIsFrame) {
// Not interested in frames.
return;
}
You need Chrome privileges for that.
In the new add-on SDK content scripts (where you don't have Chrome privileges), I'm using this:
if (window.frameElement) {
// This is an iframe.
//...
}
(See MDC doc here or here).

Controlling browser window through Javascript

I have a web app that launches an URL in other windows/tabs. I would like to check if the window/tab exists. If not, I want to create it, else I would like to pick it in the first position.
I use:
wf=window.open(address, web_form_target, 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=640,height=450');
if(wf!=null)
wf.focus();
But it goes well only the first time (in IE, not in Firefox). If I create a new tab in the window, when I call window.open() nothing happens. If I close the window it recreates it but keeps it ionized.
Is there a way I can follow to obtain a good result?
Thanks in advance.
Here's some code I've used for ages that still works as far as I know. Notice that oWindow has global scope, and that I pass it to the second parameter of open as a string, not as the object itself. Then, I test to see if it's closed before trying to open again...if it's already opened, then I just give it focus:
var oWindow;
function openWindow(p_strURL) {
if(!oWindow || oWindow.closed) {
oWindow = window.open(p_strURL, "oWindow", "status, scrollbars, resizable, width=800, height=500");
if(!oWindow.opener) {
oWindow.opener = window;
}
}
else {
oWindow.location.href = p_strURL;
oWindow.focus();
}
}
Hope it helps you find a solution,
Kevin
web_form_target is the window name.
if (wf.name !== web_form_target) {
// create it
}

Categories

Resources