closing javascript child window - javascript

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.

Related

Scripts may close only the windows that were opened by them

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.

.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.

How to refresh the parent page on closing the rad window

I have a opened a rad window using below java-script code given below. Now I want to refresh my parent page on closing the rad window. To achieve this i have linked a jquery method, OnSendInviteClose with onclose event. Now this code refreshes my parent page but some time it gives me error described as "can't execute code from a freed script". How to fix this issue or reload my parent page on close (any other way).
Thanks
var e_showaddresslist = function (sender, args) {
var url = $page.url.create("Pages/CalendarInvites.aspx?parentScreen=sendInviteWnd");
var win = $window.createPopup(url,
{
size: "750, 500", behaviors: Telerik.Web.UI.WindowBehaviors.Close | Telerik.Web.UI.WindowBehaviors.Maximize | Telerik.Web.UI.WindowBehaviors.Move | Telerik.Web.UI.WindowBehaviors.Reload | Telerik.Web.UI.WindowBehaviors.Modal, name: "sendInviteWnd", onclose: onSendInviteClose
},
function () {
//$page.get_window().set_destroyOnClose(true);
});
}
function onSendInviteClose() {
window.location.reload(true);
}
Note: All the code is place in the calendar.js file which has references in both parent and radwindow page.
Have a read of this What causes the error "Can't execute code from a freed script"
The try / catch solution may help in your case.
You can hook the OnClientColose to the RadWindow and refresh the page using document.location.reload().
function RefreshParentPage()//function in parent page
{
document.location.reload();
}
Thanks,
Saritha
Make sure the code is executed in the correct context, i.e., in the main page and not in the CalendarInvites page.
Then, if this is ok, try adding a timeout like this:
function onSendInviteClose() {
setTimeout(function() {
window.location.reload(true);
}, 0); //you can also try increasing the timeout.
}
Usually, the cannot execute code from freed script means that the content page that is being disposed is still trying to run some code but it is orphaned (e.g., other pieces of code it depends on are disposed, or its context is gone - the window object). Most often this would be the page in the iframe (i.e., inside the RadWindow), but it may also be your main page.
Attached the 'onSendInviteClose' method on close event.
var win = $window.createPopup(url,
{
size: "750, 500", behaviors: Telerik.Web.UI.WindowBehaviors.Close |Telerik.Web.UI.WindowBehaviors.Move, onclose: onSendInviteClose
}
Now create that method in the page from which the popup is opened. This function will automatically fired when popup window is closed.
function onSendInviteClose() {
//get a reference to the current RadWindow
var wndow = GetRadWindow();
wndow .Close();
// and as this method is present in parent page
// we can refresh the page controls easily.
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}

Return parent function from child function on click

How can I return a parent function from a child function on click? Here's an example code:
function returnFunction() {
var output = false;
$('a').click(function() {
output = true;
});
return output;
}
var result = returnFunction();
The result will always be false since the click hasn't happened at the time the code is being run. How could I make this work though?
My intention is to call pop-up dialogs, and I'd like to have all the logic inside one function that can easily be loaded - including the click events of a Confirm dialog box.
Elsewhere in scripts I'd be calling it this way for example:
// Menu click triggers the dialog
$('a').click(function(e) {
// The function displays the dialog and returns the click events of the dialog
var result = returnFunction();
// If result was false, we'll prevent the menu access, for example
if (!result) {
e.preventDefault();
}
});
I'm aware of the jQuery UI dialog plugin. But I'd like to achieve this without it for now.
Thanks.
An over-simplification of it is:
Everything stops (including scrolling and clicking on hyperlinks) while executing javascript. This means you cannot "pause" the script until someone clicks on a link.
The typical way of solving this is a callback function:
function my_callback(some, arguments) {
// you can do whatever in here: an ajax load, set a global variable, change some of the page, ...
console.log(some, arguments);
alert(some + " " + arguments);
}
function returnFunction(callback, param) {
var output = false;
$('a').click(function() {
callback(param, "world");
});
}
returnFunction(my_callback, "hello");
demo at http://jsfiddle.net/UnBj5/
EDIT:
I did mention global variables because they are an option, but they are typically bad style. Try to use other means if possible.
If you want more help with it, provide more details of what you are trying to do.
Try using parameters instead. Send a parameter to a function that shows your alert boxes, show a different pop-up alert depending on the parameter, what you are trying to do won't work because its basically a chicken-egg problem.

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