hello i have a Django base app and one of its apps is chatting with customer service.
i want when the customer clicks on the the [x] icon or close the window, a small popup window should come up that include:
1.a string like "thank you, and we hope you can take time and answer the survey:"
2. a button that directs the customer to the survey page.
i have this part of the chatting app java script file:
window.onbeforeunload = function() {
window.open ("http://gadgetron.store/male_chatbot/popup/"); <= i have tried it and it doesn't work
$.ajax({
type: 'GET',
url: 'http://gadgetron.store/chatbot/run_python_clear_chatM/',});}
thank you,
I tried some thing like that before, but I think the browser disabled the popup customization. and note that you need configure the ajax call as synchronous (check this). you can use window.onunload event handler, and then small popup with confirm and cancel button, if you click on confirm you script will be executed and the tab will closed
For user experience reasons, browsers do not allow opening Popups during the unload-phase, which this event is part of. The only thing you can do is open a popup, asking the user to confirm that he really wants to leave the page. This is achieved by returning any non-empty string from the event callback (ie. return "Are you sure you want to leave?"; where you currently call window.open). The text inside this confirmation window can no longer be influenced by the application for most modern browsers.
For information regarding popups during the unload phase, have a look at the WHATWG Spec, most importantly the ignore-opens-during-unload counter which blocks document.open when set to any value greater than 0.
Related
I have site which contains many links. User can open links in new window. User can log out from one window. If they do so and click on the other window of my site then I want to give an alert to the user saying that "Please login to continue". My questions are:-
Is this possible in Drupal 7?
Is there any extra module to support this.
If there is no contributed module available for this then my idea is to register a click event on the page(document) inside JQuery which will always check if user is authenticated or not. I want to know that which function in Drupal will help me in understanding authenticated user of site in JQuery.
Please share if any other option available to achieve this goal.
This is not an easy task, but I am going to explain how I would do so (without too many details):
Create a web service which receives a cookie and returns whether it represents a live session or not.
Use the visibility change javascript event to detect when a user leaves the browser or comes back in. When you notice the user has come back to the browser, perform an AJAX call to the server you set up in step one. You should provide the cookie stored in the browser, and expect a boolean answer.
Depending on the response given by the server, display a modal dialog with Javascript showing a login form orwhatever you consider necessary.
Note: This method will only work if the user is using a browser supporting visibility change events, and can be bypassed disabling Javascript code execution. In addition, the user will still be able to see the content if the browser is not the focused window (for example, there are two opened windows, each of them covering one half of the screen. The event will not fire until the focus comes back to the "forbidden" window).
I am working on a examination project, When user starts a test it open in new window by window.open(). If user close a test before it finish then session creates a problem so I want to clear session when user close the browser window or hide/disable close button before test completes.
nothing you come up with here will not be overrulable by the user; welcome to client-side programming (the actual browser "close" button is not within your reach). You can tap into onclose (lots of sites do this in order to pop up a confirm("do you want to leave this page?") dialog) but even that is very easy to bypass by anyone who knows how to open a browser console (F12 on every browser, for example) because they can just redefine window.confirm = (() => true); and not get bothered by confirm dialogs.
Partial solution would be to use the onbeforeunload and send a request back to the server once it is invoked.
Once the request is received on the server, you can destroy the session.
Again, this is easy to bypass on the client side but does provide extra functionality.
window.onbeforeunload = function() {
// AJAX to server
};
You could use this answer here:https://stackoverflow.com/a/1119324/1489237
That would give you that message "are you sure you want to leave this page?"
On that function,instead of a message you can submit an ajax that will call a script to do a session_destroy() before closing the page.
I think you can set session expiration time to be few seconds? and perform ajax requests to maintain it, while student is answering questons
Can you use popup block with iframe inside?
<div id="test-popup">
<iframe src="/test/12345">
</div>
And open it with JS:
$('#test-popup').show();
This is for example only. You need more CSS and JS to make it works.
Currently I have an application where a user hits a clicks on a URL and goes to my form. The user then enters his information into the form field, which is then sent through jquery ajax to a PHP script which enters it into the database. Upon success callback, it would alert the user that they had been registered and closes the current browser tab. (Let's just say I need the closing the browser tab behaviour to persist).
$.ajax({
type: "POST",
url: 'goToPHP',
data: data,
success: function(data){
alert('You Have Been Registered Successfully');
open(location, '_self').close();
},
error: function(data){
}
});
I understand that most modern browsers (Chrome included) are limiting the ability for javascript to only close tabs/windows it created for security reasons. Temporarily I had used open(location, '_self').close(); to get around the issue, but alas, it seems chrome's most recent update prevents you from doing this as well (Prompts you with a warning: 'Scripts may close only the windows that were opened by it'.)
Is there a way around this? I'm not talking about something along the lines of:
open(location, '_self').close();
But something that will work all the time on Chrome (e.g. changing a Chrome setting to allow scripts to close tabs/windows (similar to how this could be done through about:config in Firefox) or a way of restructuring how the user hits the form, so that the window object is available in javascript so i can call windowObject.close(); ) .
Thanks in Advance!
A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document.
http://www.w3.org/html/wg/drafts/html/master/browsers.html#dom-window-close
In effect this means that, in common usage, you can only close windows/tabs that you have created in JavaScript. However, it is also possible to close a window or tab (but not a frame or other nested browsing context) as long as that window or tab has no history. Usually, this is a highly unreliable feature to make use of as you generally cannot realistically expect your user to have opened no documents in their browsing context before opening the one that you are trying to close; however, there are certain cases where you can safely assume that the page is in a context with no history, but generally these cases would fall under the 'opened by script' categorization as well.
Additionally, if you use a link to '#' for JavaScript purposes or any other sort of bookmark linking within your page, that will populate the session history and render the context unclosable unless you use JavaScript to prevent the hyperlink event from being executed. For purposes of JavaScript, it is probably better practice to just use javascript:void(0) if this is an issue.
In your case, the only way you can guarantee that you can close the tab/window is if you can guarantee that your form is being opened in a browsing context with no history or if you can open the form page via JavaScript. Whether or not those limitations are reasonable to work around depends on the specific structure/implementation of your website.
You can only close windows/tabs that you create yourself. That is, you
cannot programmatically close a window/tab that the user creates.
For example, if you create a window with window.open() you can close
it with window.close().
To make your code work, you should open up a window using JavaScript and then you'll will be able to close it by code.
You can use something like the below example code.
<script>
function openwindow() {
var pop = window.open("localhost","Ratting","width=550,height=170,0,status=0,")
var close = function() {
pop.close();
};
setTimeout(close, 2000);
}
</script>
<body>
<a href="javascript:void(0);" name="Window_Name" title="title_here" onClick="openwindow()" >Click here to open the child window</a>
</body>
you can use this concept on your ajax URL and ajax success stage.
$.ajax({
type: "POST",
url: function(){
var pop = window.open("localhost","Ratting","width=550,height=170,0,status=0,")
},
data: data,
success: function (resp) {
var close = function() {
pop.close();
};
setTimeout(close, 3000); // close after 3 seconds
},
error: //error code
}
});
HTML
<a class="closeButton">Close</a>
JavaScript
$('.closeButton').click(function () {
close();
});
You dont must use window.closed() or reopen tab and closed.
Just use this code.
For me its working.
I used to use the window.open(...).close() trick (which no longer works in Chrome or FireFox). In my case, there is a quit button in the (Java) application that, rather than trying to close the browser tab, cleans up, redirects back to the starting page and terminates. The user can then decide to:
manually close the tab
leave it open, or
re-enter the application
I realized that my real goal is not to close the browser tab, but to inform the user in an obvious way that the session is over.
Using Javascript is it possible to hook a custom function to the browser back button while preventing the default event?
So simply put, the user clicks the browser back button, and instead of being taken back to the previous page on my site, my function fires. My aim is not to to prevent the user leaving my site.
Something like the below pseudo code:
window.backbutton.click(function(e){ preventDefault(e); myFunc(); })
FYI What I actually have is an internal back/forward system controlled by buttons within the view and I'd like to trigger my buttons when user clicks browser back.
You should look into Browser History https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history . It is a new standard for emulating pages (back and forward) on a single-page site.
you can do this
$(window).bind("beforeunload", function() {
return "Are you Sure you want to do this even though there are internal navigation buttons? if you do whatever you have done so far
will be gone unless it has been completed and/or submitted "; //
Return whatever message you want
});
Edit: this method displays a confirmation with the message and
automatically places buttons in the confirmation window
the author also clearly stated he does not want to prevent users from
leaving his site
see js fiddle http://jsfiddle.net/JW9fX/
Aaron - "updated question, I do not want to prevent them leaving my
site – Aaron 27 mins ago"
Edit: there is no definitive way to display a message only on the back
button in my knowledge i have also looked and have seen the only
methods that pick up on the back button are
$(window).bind("beforeunload",function(){return;}) and
$(window).onbeforeunload = function(){return;} and they both pick up
on when the page is refreshed or any other navigation away from the
current page. This is currently the best answer that can be provided
in the year 2014
Edit:
above is for all unload events
http://jsfiddle.net/Lhw2x/85/
been messing with that through all its lower objects it cant be done through javascript till someone decides to make all major browsers use a specific name for each unload event instead of it just being an unload event
so the answer is no you cannot hook a custom function to the browser back button while preventing the default event using javascript
I am looking to develop a small popup message which acts similar to the window.beforeunload function, to notify the user, that if they leave the current page, they will lose all of their data.
However the issue with the beforeunload event is it fires to often.
I would like to have the popup message fire only when a user closes the page, or clicks a link which takes them away from the current page, to ensure they are aware that their current action will result in the loss of the form data they have entered so far.
However beforeunload event goes further to fire when they refresh the page, which is not needed for this case, and also when the forum is submitted.
Could anyone advise me on the best way to develop this. I thought about using a basic confirm dialog and have it fire under the right circumstances, however is it possible to know if the user is refreshing the page, and if the forum is being submitted (without jQuery).
How can I have this dialog fire at the appropriate times?
Unfortunately, I don't think this is possible. The page unload events are very limited, for security reasons.
If you only want it to appear if the user added or changed formdata, why not check for changes in the data? If yes then return the question on beforeunload, if not do nothing.
Assuming that the form isn't too complicated, you could save form data by using Ajax call, which means there will not be a page reload. So, beforeunload will then behave as it was designed to.