No, I'm not trying to make an annoying popup. I have a simple webpage that is a web-based control for a SOA tool that will allow our clients mobile access to systems we put in their locations. If the user closes the webpage without using a "Disconnect" button that closes the communication tunnel on the service side before closing the window itself, the system remains active until the service times out. That may not sound terrible, but the same communication tunnel is used by in-house staff in high-priority situations, and if they cannot access it because the customer's web service is tying it up, that is a Very Bad Thing.
So, I want to prevent the user navigating away from the page or closing the tab or browser instance by any other means than clicking "Disconnect". I'm sure it's possible, I just need a nudge in the right direction. The solution must be as browser-agnostic as possible, especially concerning mobile browsers.
You can try using the onbeforeunload handler. but it is not always thrown.
See fiddle using jQuery: http://jsfiddle.net/maniator/qpK7Y/
See fiddle with pure javascript: http://jsfiddle.net/maniator/qpK7Y/10/
You can't stop the using from closing the window via the close button at the top right. You can force a child window to retain focus like this though:
Declare a global variable:
var childWindow;
Assign the variable to a window object
childWindow = window.open("...", "...");
if (childWindow){
childWindow.focus();
}
To force focus on the child window, use the onfocus event on the parent:
focusChildWindow = function(){
if (childWindow != null){
childWindow.focus();
}
}
<body onfocus="focusChildWindow();" ...>
Related
I want to close window tab after some success action, because customer don't want to close it manually
Script below
<script type="text/javascript">
window.close()
<script>
closes only the current View(), not the whole window.
But if it places in "_Layout.cshtml" at the first start the script is processed as expected. But after several redirects, and then when returning to "index.cshtml", an error appears
Scripts may close only the windows that were opened by them.
I know this is javascript defensive constraint, but I'm not good with js, and I don't know in which case this error appears and how to bypass or avoid it
To avoid annoyances and bad practice, browsers disallow Javascript to close browser windows unless they detect a clear action to do so (i.e. the result of a button press). If you're looking to close the window after some async work, its very likely you won't be able to do so.
You should try adding a popup window that gives the user the option to click a button which will close the tab if that is enough. You can add the following to the button to make it do so:
onClick="window.close()"
and it should work just fine. Failing that, try using an a tag like
Close
If you absolutely can not live without this feature, you'll need to create an extension for each browser you want to support that will close the window upon request. Beyond that, you'll need to find an exploit.
Context :
I have developped an application which require authentification. This application uses events for dialoging with a server. When the server answer, some events are send to the client (UI).
Problem :
When the user close the page, it is necessary to make a logout on the server. With my architecture, it's easy to call a method which perform this logout. But i would like that the user show the logout progress before closing the webpage. In fact, i would like to close the webpage only when a specific event (for example : disconnection_success), is well received.
Moreover, it's verry important to not launcg another webpage because event is received on the first webpage when the logout is successfull. (Because dialog is done throw XMLHttpRequest)
Test :
I already do some test using onbeforeunload but it seems that is difficult to customize the popup.
Do you have some ideas to resolve the problem ?
BR
There are some issues with this, but you're on the right track. You're right in that you should use onbeforeunload because it is the only event that you can have triggered upon the closing of the browser window. (I know you can use onunload but at that point you have no time to do anything.) The issue here is how much code do you want to execute. The onbeforeunload doesn't allow you much time before it starts to unload the page.
BTW, there are two different scenarios with onbeforeunload:
If you return a string inside the onbeforeunload event, it creates the pop-up that you were referring to. The issue here is that with the pop-up, you won't have enough time to execute code
The other option is not returning anything. Instead, call your logout methods. This should give your code enough time to execute before closing
I actually had a question very similar to this and ended up solving it myself: How to logout during onbeforeunload/onunload using Javascript
In your question you state that you want to have a progress bar displayed when they log-out. This is impossible to do when the user closes the browser. At the moment they close their window, you have lost all control, except for in the onbeforeunload (and onunload but don't use this), and that is why your code needs to be executed there. With that being said, you could anchor your logout button (I'm assuming you have one on your application) and have it display the progress bar.
Just think about what could happen if you actually did have that kind of control - where you could pop up windows and progress bars when the user is trying to close their browser window. You could pop up anything and restrict the user from having any reliable functionality. That is why it was programmed that the onbeforeunload (and unload) events are the only ones possible to access the closing of a browser. These events have some pretty strict guidelines to them that prevent any kind of possible mis-use. I understand the problem you're having, I was there and it stinks, but I think that is your only option if you were going to use onbeforeunload.
How can I move my popup window back or minimize it? The following code snippet doesn't work:
w = window.open('asdasd');
w.blur();
window.focus();
I need the same effect as in http://www.filesonic.com/file/2959312855/CoreczkaArena.rar, when you Click "SLOW DOWNLOAD" the popup with ad moves back under the main window.
Got You Then - Here The Solution
var newWindow = window.open('Default.aspx');
newWindow .opener.window.focus();
You should not do this.
The reason for that is that your users' experience will suffer and that this type of behaviour is often considered harmful.
If you want to put some content "in the background", just do it within current page by implementing CSS and JavaScript scripts that make site behave as it would contain multiple "layers" or "windows". Try using jQuery UI for example.
If you insist on doing what you asked (creating new window and moving it in the back), someday it will probably stop working and your content will be blocked by popup blockers. But before that happens, you will probably receive negative feedback from the users of your site.
There is not necessarily only bad intentions to want this type of behavior of the browser.
My web application is running on Chrome.
I need to launch http requests to call a web service in localhost for document management from the document server.
And I would like to avoid cluttering the user's desktop with extra windows.
With Window.open you can reduce to the size 100/100 but you can't minimize totally when launching the window.
On the other hand, manually, you can do it afterwards.
I don't see why it can't be done directly when the calling site is in https
Cpf
How can we detect when a user opens a new window. The user is already authenticated and we make heavy use of sessions.
We were trying to avoid Ctrl+N javascript hooks but maybe that is an option.
I am assuming the request is the exact same URL...with Ctrl+N?
We were trying to avoid ctrl-n javascript hooks
Forget it. Whilst you could in theory try to catch keypress events for ‘n’ with the Control key modifier, there are any number of other ways to open a new window or tab which may be more likely to be used, and you won't be able to catch. File->New Window/Tab, middle click or shift-click link, middle click back/forward buttons, right-click-open-in-new-window, open bookmark in new tab, double-click browser icon...
The user is already authenticated and we make heavy use of sessions.
That shouldn't be a problem in itself. I guess what you mean is that your application is dumping all sorts of page-specific data in the session that it shouldn't have, and now you find the application breaks when you have more than one window open on it? Well, commiserations and happy rewriting.
In the meantime about all you can do is tell the user “please don't try to open two browser windows on the same application”. There are potential ways you can make JavaScript on one page notice that JavaScript is running on another page in the same domain at the same time, generally involving using document.cookie as a inter-page communications conduit. But that's also a bit fragile.
If opening a new window causes a problem in your application, then you should fix the application code to handle it instead of trying to apply an inconsistent and unreliable client-side "bandage". That's my opinion.
Why?
And anyway you can't detect it. User can open new window not only with Ctrl+N but also with File->New Window.
You could possibly put a window count into the session and increment it on window.onload and decrement it on window.onunload.
Imagine me tutting, sucking air through my teeth and going "better you than me, guvna" if you use that, though.
What I have done to solve this issue is when the user authenticates set the window name on valid login.
<script>
window.name = 'oneWindow';
</script>
And then on the master page do a javascript check:
<script>
if (window.history.length == 0 || window.name != 'oneWindow')
//history length to see if it's a new tab or opened in a new window 0 for IE, 1 for FF
//window name to see if it's a CTRL + N new window
</script>
If the check is true then hide/remove the main content of the page and show a message stating they are doing something unsupported.
This works when your login page is not tied into the master page.
If you do not have a master page then I would suggest putting the check on all your pages.
Yes and no,
You'll always see it if a control has focus, else the event is sent directly to the browser and the code on the page never hear about it.
In my experience you can't hijack the browser's shortcut, your mileage may vary. You are likely to know it happened but the browser will do its thing (for obvious reason)
In most browsers, the effect of Ctrl-N is to open a new window at the same URL as the old one and associate it with the same sessionID.
Your best bet would be to modify the back end code if possible and allow for such things. Breaking the browser's feature is never a good thing.
I am writing a web application that will run in kiosk mode on a touch screen. I am currently only targeting it for running on Firefox 3. A few of the use cases I have need to visit external sites. I wish to do so with an embedded browser, which I'm tackling with the help of an <iframe>. I need back/forward buttons for the embedded home page.
I've managed to access the history object of the iframe with
var w = document.getElementById('embeddedBrowser').contentWindow;
w.history.back();
The history of the embedded window is the same as that of the parent window. Therefore for a newly loaded <iframe>, this call will go back to the previous page of the system.
Is there any way to avoid this or a more correct way of solving this?
Because there is only one history object shared within each tab this seems impossible. The proper way around it would be to test window.history.current or window.history.previous before calling back. Unfortunately, window.history.current is privileged and so not available to unsigned pages.
Here's a rough sketch of a messy workaround:
<iframe src="somepage.html" name="myframe"></iframe>
<p>Back</p>
<script type="text/javascript">
document.getElementById('backBtn').onclick = function () {
if (window.frames['myframe'].location.hash !== '#stopper') {
window.history.back();
}
// ... else hide the button?
return false; // pop event bubble
};
window.frames['myframe'].onload = function () {
this.location.hash = 'stopper';
};
</script>
Of course, this is assuming that no (#hash) browsing ever goes on in parent window, and so on, but it seems to work for the problem of limiting back movement.
You might want to take a look at Adobe AIR. It lets you write your application using all the same tools / languages (ajax, html, etc etc), but since it runs as a desktop app and not in a web browser, you have more control over things, such as embedding browser frames and knowing exactly what they are doing, what URL it's going to, controlling it's history, etc. Look here for a few pointers on getting started.