Force Close Webpage - javascript

I have a page (let's say page1) that open the page2 using:
showModalDialog(page2, '', 'dialogWidth:55em; dialogHeight:50em; status:0');
I am facing 2 problems (in this post i am going to write the first one):
1) when i want to close the second page using javascript code it always asks me that the page is trying to close itself do i want to allow that or no. I want to force close it without asking me anything. I have tried the following:
function CloseSelection(id) {
window.opener = "page1"
window.returnValue = id;
window.close();
}
And in the Form tag target="_self". But nothing happened, the popup keeps on showing.
can anyone help me please?

You dont need to set the window.opener as it is set by default to the object of opener page.
On Page2 the code will be:
function CloseSelection(id) {
window.returnValue = id;
window.close();
}
On Page1
var returnVal = showModalDialog("page2.html", '', 'dialogWidth:55em; dialogHeight:50em; status:0');
// here returnVal will contain the data which is return by page2
Hope it helps.

You can not force the window to open in a new window. It might work in some browsers but not all.
In order to close a window you can do this:
// open
var win = window.open("someurlgoeshere");
// close
win.close();
I believe you only get the "are u sure you wish the leave this page" message if the window itself tries to close itself (window.close())

Related

I need to reload current location js

I need to reload current location(href) via JS :
I have a button that open a new window and when that window closed I need to reload the location in window that was initiator(parent). It is work in my browser, but one said me that his window does not reload( he has last version chrome and mac book)
I have the followin code:
var loginWindow = window.open(...);
loginWindow.focus();
loginWindow.onbeforeunload = function() {
location.reload(); //when the popUp window close
//I even tried this:
window.location.href = window.location.href;
}
but the page not reloaded
AS onbeforeunload does not work here it is recommended to check if the window exists with certain interval Please find the reference link below
Check this post

Window.open only if the window is not open

I have a link on my site that opens a new window to a page that plays a very long audio file. My current script works fine to open the page and not refresh if the link is clicked multiple times. However, when I have moved to a seperate page on my site and click this link again, it reloads. I am aware that when the parent element changes, I will lose my variable and thus I will need to open the window, overiding the existing content. I am trying to find a solution around that. I would prefer not to use a cookie to achieve this, but I will if required.
My script is as follows:
function OpenWindow(){
if(typeof(winRef) == 'undefined' || winRef.closed){
//create new
winRef = window.open('http://samplesite/page','winPop','sampleListOfOptions');
} else {
//give it focus (in case it got burried)
winRef.focus();
}
}
You should first to call winRef = window.open("", "winPopup") without URL - this will return a window, if it exists, without reloading. And only if winRef is null or empty window, then create new window.
Here is my test code:
var winRef;
function OpenWindow()
{
if(typeof(winRef) == 'undefined' || winRef.closed)
{
//create new
var url = 'http://someurl';
winRef = window.open('', 'winPop', 'sampleListOfOptions');
if(winRef == null || winRef.document.location.href != url)
{
winRef = window.open(url, 'winPop');
}
}
else
{
//give it focus (in case it got burried)
winRef.focus();
}
}
It works.
Thanks to Stan and http://ektaraval.blogspot.ca/2011/05/how-to-set-focus-to-child-window.html
My solution creates a breakout pop-up mp3 player that remains active site wide and only refreshes if the window is not open prior to clicking the link button
function OpenWindow(){
var targetWin = window.open('','winPop', 'sample-options');
if(targetWin.location == 'about:blank'){
//create new
targetWin.location.href = 'http://site/megaplayer';
targetWin.focus();
} else {
//give it focus (in case it got burried)
targetWin.focus();
}
}
Like you said, after navigating away from original page you're losing track of what windows you may have opened.
As far as I can tell, there's no way to "regain" reference to that particular window. You may (using cookies, server side session or whatever) know that window was opened already, but you won't ever have a direct access to it from different page (even on the same domain). This kind of communication between already opened windows may be simulated with help of ajax and server side code, that would serve as agent when sharing some information between two windows. It's not an easy nor clean solution however.

Detect if new window if opend using javascript

Creating a small chat application where user can start chat by clicking on chat button next to user's profile.
Once anyone will click on chat button a popup window will appear. Now I want to detect dynamically if this popus is still open or closed.
If popup is opened= Display new message alert in notification.
if popup is closed = Do not alert new message in notification (As chat window is already opened)
This is what I tried:
Chat with user 2
Chat with user 1
// on document ready
setInterval(function(){
chkAlerts();
}, 10000);
var newwindow = null;
function popitup(url) {
if ((newwindow == null) || (newwindow.closed)) {
newwindow = window.open(url,'Buy','width=950,height=650,scrollbars=yes,resizable=yes');
newwindow.focus();
}
else {
newwindow.location.href = url;
newwindow.focus();
}
}
function chkAlerts(){
if ((newwindow == null) || (newwindow.closed)) {
//Do something
alert('now window is closed show alerts');
}
}
With this code I am always getting
alert('now window is closed show alerts');
chkAlerts will always show the alert as the initial value of newwindow is null.
Presuming you only want the alert after a chat window has been closed tweak the logic to ignore the initial null state;
function chkAlerts(){
if ((newwindow != null) && (newwindow.closed)) {
//Do something
alert('now window is closed show alerts');
}
}​
Your error is not in the javascript code, is in the anchor, you are closing the double quotes inside the call to the function, they must be like this:
Chat with user 2
Anyway, but also, as you can see, you are only storing the last opened window, if your interest is in know that both are opened you should create newwindow as an array.
Cheers,
Continued from comments:
Just so you know, when reloading or navigating away from the
window-opener page, the reference to newwindow will be lost.
Yes jan, this is the actual problem, do you think is there any
function in javascript to do this... or do i need play around with
session or something?
Not even possible with sessions or any other server sorcery.
Your option if you really need to keep track of the chat window is to include the chat on your "opener" page and make it an inline AJAX chat.
You could have an AJAX listener on all your pages and "open" that chat window on all open tabs/windows of your site, so that the user doesn't lose track of it.

Executing function when pop-up window with redirects inside is closed

I'm showing the pop-up window from the page. The popup has one page, which then redirects to another (LinkedIn authorization one, to be clear), and then, after the successful login, the initial authorization page is opened again.
I want to reload the parent page when the popup is closed, but cannot do this.
The code is the following:
function OpenAuthorizePopUp() {
var w = window.open("AuthorizePage.aspx", "PopUp", "width=450,height=540");
w.onunload = function () {
SubmitPage();
};
return false;
}
function SubmitPage() {
alert("SUBMIT!");
}
The issue here is that SubmitPage() function is called not when window is closed, but just after the popup is shown. I guess it's because of redirect inside the popup, and unload is raised when we move from the first page.
Is there a way to catch the actual moment when the window is closed in this case?
So the flow of the popup goes like this:
1. Your page -> 2. LinkedIn Page -> 3. Your Page
Can you change the URL that LinkedIn sends you back to (3.) ? If so, have LinkedIn send you back to a page that has window.onunload = window.parent.SubmitPage; on it.
If you can't- ie, LinkedIn always sends you back to the first page- make the third page test for a successful connection to LinkedIn and if it's present, execute window.onunload = window.parent.SubmitPage;.
A possible improvement to your idea would be to just have the "success" page call window.parent.SubmitPage(); and then window.close(); since you won't need the popup anymore.
Found the solution. Instead of accessing parent window with window.parent, wrote
opener.location.reload(true);
And it works.

check if window is open and opens a new window only for once?

i use the code below to open a new window when a user drops by my site using jquery document ready function.
<script type="text/javascript">
$(document).ready(function(){
window.open('link','title','width=460,height=500,status=no,scrollbars=yes,toolbar=0,menubar=no,resizable=yes,top=460,left=600');
return false
});
</script>
However , it keeps on popping out on every single page that have this code.
What i want to do is only pop out this window ONCE for users that do not have this window opened.
If a user have this window opened and it will no longer pop out a new one.
So how can i do this??
give the window a name
If a window with the name strWindowName already exists, then strUrl is loaded into the existing window.
just make sure your window openers use the same name so that they open in the same window if the window with that name is already open. (wooh! tongue twister!)
You can try to add a cookie to the users who have the window open. Check it on every page. And don't forget to erase it when the window is closed.
About JS cookies
This may help.
http://www.javascriptkit.com/javatutors/openclose.shtml
Use browsers' cookies. It is a way to keep some "variables" across multiple pages that you can manipulate on your domain only.
Since you're using jQuery, I'd suggest you use this plugin.
This way, the code is as simple as:
$(document).ready(function() {
if ($.cookie('opened') !== null) {
window.open([...])
$.cookie('opened', 'is')
}
})
<script type="text/javascript">
$(function () {
//get the complete queryString (url) for the popup page
var url = document.URL;
//use sessionStorage to control whether the page has been opened or not
//try get the sessionStorge name, if = nothing, open page
if (sessionStorage.getItem('visited') == null) {
//then set a sessionStorage name and value,
//it is important that this line appears BEFORE the window.open
//else you will get a loop because ==null then is always true
//at last set a sessionStorage value so it no longer ==null
//and open window - once.
sessionStorage.setItem("visited", "Ja");
window.open(url, '_blank', 'width=750, height=1010');
}
});
</script>
This popup window will open only once, no cookies stored in the client browser and the next time you open the browser, the sessionStorage is gone. (The $.cookie('opened') sample above does not work!)
Kind regards,
Ola Balstad
Add a variable to the file like a Flag
<script type="text/javascript">
var myWindowFlag = false;
$(document).ready(function(){
if(!myWindowFlag){ window.open('link','title','width=460,height=500,status=no,scrollbars=yes,toolbar=0,menubar=no,resizable=yes,top=460,left=600');
myWindowFlag= true;}
return false
});
</script>
this might help you
EDIT: we can do as #devjosh said
<script type="text/javascript">
$(document).ready(function(){
if(!getCookie(myWindowFlag)){ window.open('link','title','width=460,height=500,status=no,scrollbars=yes,toolbar=0,menubar=no,resizable=yes,top=460,left=600');
} return false
});
</script>
and in your new window reset the cookie value onload

Categories