Hy guys,
i made a upload page, after the sucess of operation i would like to close the html page automatically or by a link or something.
window.close don't work in this case, 'cause is not a window pop up.
Any ideas?
Best regards,
Valter Henrique.
There is no way to do this and that's a good thing. It may confuse the user. Instead, show a message telling the user that they may close the window now.
I see you want to close the greybox from within the child page. Try
top.GB_hide();
If window.close() won't work, you can't do anything else.
Which is good, because I don't want sites closing my window.
You could try window.close(), and then immediately after it update the DOM with a message such as "This window may be closed." That way, if you ever can close the window (perhaps some browsers may let you), you will cover both bases.
some kind of hack here...but may not work in all browsers
window.opener=null;
window.open('','_self');
window.close();
Related
I have request from the customer to adjust some old functionality in the system. The current file has href links that look like this:
#Name#
If you look the code above you see that target="_blank" page will be opened in the new browser window. However, user wants to be able to close that window if they click OK/Cancel button in page.detail.cfm. I tried using this code for closing the browser window:
var closeBtn = document.getElementById('btn_cancel');
closeBtn.addEventListener('click', cancel);
function cancel(){
window.close();
}
After I tested the code and clicked Cancel message in the dev tools looks like this:
Scripts may not close windows that were not opened by script.
I guess that window can't be closed if not previously opened with JavaScript. I'm not sure what would be the best approach to solve this issue? Thanks for your help.
Technically a script is not allowed to close a page that a user has opened as opposed to it being opened by a script itself. It's a browser security issue. I know there were some hacks for it but thing like this get patched pretty quick from what I can tell. You could technically open the window with a script instead with some sort of click event or such, but again this is a bit of a work around. Check this out https://developer.mozilla.org/en-US/docs/Web/API/Window/close
I have MyPage.aspx html page (generated using ASP.Net). When user tries to navigate away from this page, I need to close the window – user should not be able to go back or navigate to another page.
When I used window.close() inside window.onbeforeunload event, it asks for a confirmation to the user. “The webpage you are viewing is trying to close the window. Do you want to close the window?” On clicking “No” the user can escape the close attempt. Is there any way to forcefully close the window without giving an option to the user?
Reference:
How can I close a browser window without receiving the "Do you want to close this window" prompt?
Html javascript to open new window and close current window
"Unknown Exception" when cancelling page unload with "location.href"
Display confirmation popup with JavaScript upon clicking on a link
You can "trick" the browser like this:
window.onbeforeunload = function() {
window.open('', '_self', '');
window.close();
}
It seems to work in chrome/safari/ie/ff: http://jsbin.com/olijig/1
Firefox seems stubborn, but there might be another way to do the same in FF.
I should probably say that this technique is in no way standard and I don’t recommend it at all, and this code might break in many browsers besides firefox.
UPDATE
It actually works in Firefox too (latest version), but not older versions (I tried 3.6.1). You need to do some more testing to confirm the browser compatibility.
No, you can't. The user must be always capable of controlling whatever happens in his browser.
I'm not positive about this, but I believe if you have a window open another window, the parent window can close that child window. Would it be practical to have a landing page that opens your app in a separate window that could then close the window through javascript? Someone can probably elaborate more, as I haven't done this myself.
I am using window.open() method to open a page as a pop-up window for a link button click event.
But the poup-up window is having minimize,maximize,close(x) button.
I dont want those buttons. How can remove these buttons?
This is the method i am using,
window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,titlebar=no,scrollbars=1,resizable=0,width=450,height=310,left=500,top=350");
Tell me how can do this.
Regards,
Chirag Jain.
You can't.
If you want a popup style window without full window decorations you'd have to create a new overlay <div> on top of the existing content and fill that with content, perhaps using an <iframe>.
You can't do it from javascript alone. Think about it, if you could, then people could put it into code on web-pages and cause other people's computers to open windows they couldn't easily close.
Instead you'll have to look for an answer specific to whichever browser you're using to host this application, and change it on the computers of your users appropriately. Even then though I don't think you'll be in luck (with Firefox for example, I can see how to get rid of them on all browser windows, but not on just one).
Is it possible to redirect to another page when the user closes the browser?
Attempts:
I tried onunload, does not work
window.onunload = function redirect(){...}
I also tried another method, it doesn't work either:
window.onbeforeunload = redirect(){...}
<body onbeforeunload="return false; redirecty()">
The 3rd method, I want to cancel the onbeforeunload (means delay closing the browser), the I call the redirect function, window.confirm, if yes redirect, if no then close the browser. But it does not work as well.
Is there any other way?? Maybe prompt to let user select whether to redirect to new page when he/she close the browser? I'm running out of ideas...
Your onbeforeunload event needs to return a string (that doesn't equate to false), the browser will include this string in its own message displayed to the user.
window.onbeforeunload = function(){
location.assign('http://www.google.com');
return "go to google instead?";
}
However, it's going to be really tricky to word your message in a way that the user would be able to understand what to do. And I'm not sure this is robust in every browser, I just tried it in Chrome, it worked, but I ended up with a tab I could not close! I managed to kill it via the Chrome task manager thankfully.
It's not without it's faults but it works
window.onbeforeunload = function(){
window.open("http://www.google.com","newwindow");
return "go to google instead?";
}
This will open a new window as a popup to the address you selected when the user closes the page, though it is limited by any popup blockers the browser may implement.
If the user is trying to close the browser, then his intentions are pretty clear; he expects that the browser will close. Preventing that from happening, or causing anything else to happen in between the user clicking on 'close' and the browser closing is just a bad idea IMO. Is there a special reason for this? I mean, when I click on the 'close' button I expect that the browser will close, and should anything else happen, I would find that extremely annoying. I think I'm being reasonable enough. Am I? Who knows such things.
Why don't you try to entice the user to visit the other page in a less intrusive way? Like with a link or a banner?
The simple answer is no. If browsers allowed you to do more with the onbeforeunload/onunload events, this could be used pretty maliciously by anybody.
window.close();
The above will pop up a confirm dialog each time, and not working at all in Firefox.
Use this way
window.open('','_self');
window.close();
I think that's not without a reason. People don't like windows being closed without notice.
Closing a browser window is not as straight-forward as it used to be years ago.
Typically, a newly opened window can be closed if:
1. the 'close' is called within the DOM of the window itself
2. the closer is the opener
However, with almost all browsers having tabs, if the opened window is the only remaining tab in the main window, it might not close without prompt for above case 1. Even if it closes, it might just close the tab and leave the main window opened.
You can't - it's a security feature. You'll need to look into showing some form of modal dialog if you wish to be able to close it. Have a look at something like This JQuery Example which features auto-close
Windows not opened by JavaScript cannot be automatically closed with JavaScript (and I can't think of any good reason for a website to close the window that the visitor arrived at the site with, discarding their Back history in the process)
it works in chrome,firefox need to turn the "allowjavascriptclosewindow" option on.
ie need alter your code as:
window.open("","_self");
window.close();
The below javascript works fine to close the tab with user confirmation.
<script>
function closeWindow()
{
if (confirm('Are you sure you want to close the Window? All the unsaved data will be lost')) {
top.window.open('','_self','');
top.window.close();
}
}
</script>