How to make a postback when the user clicks the back button? - javascript

I have a webforms web app, and all I need to do is, when a user clicks on the browser back button, I want to make a post-back or reload my page so it makes the post-back itself so new data is loaded.
I am using a library called jQuery-backDetect
which allows me to detect browser's back button click and I try to do is to make a post-back using __doPostback('arg1', 'arg2'). But it won't work at all.
And the strangest thing here is that, if I have the browser's debugger (in developer tools) open, or I pause the code execution using a breakpoint, it works perfectly. Here is a simple code I have written:
$(window).load(function(){
$('body').backDetect(function(){
// Callback function
debugger;
__doPostback('arg1', 'arg2');
});
});
I have tried to go through all the question here but they didn't help.
As strange as the question sounds, I really hope someone can help me. Thanks

You might consider wrapping the page content in an UpdatePanel, that might help create the behavior you are looking for.

Related

Change back button behaviour of browser

Posting without a target so that a web page reloads seems useful behaviour for some things - such as writing a login page. I have implemented a calendar in PHP which takes advantage of this. It reloads an object from the session (or creates a new one if not present), applying any changes that result from the post then saves the object back to the session. The problem is this. If I hit the back button I don't want to go back through every click of the calendar button but would rather jump back to the page before arriving at the calendar page. Not only that, if I do go back one calendar page after another I get an annoying "confirm form resubmission". I have implemented an incrementing value after the # for each post so that I might be able to use window.onhashchange. The problem is that window.onhashchange never fires so I am unable to intercept the back button and pop the history stack. Any ideas? Am I better off coding on the server side with javascript?
Well I solved one problem. My form subclass in PHP defaults to using POST as I understand this is more secure. This causes the annoying resubmission problem when using the back button. I now use GET in my calendar page which solves this issue. I am still bemused by JS debugging in Netbeans. I have never got script to stop on a breakpoint within a single document. I have previously had it working with an external javascript source but this no longer works. If I can output to console but there is no window in which to see the output. I am told window.alert no longer works for some events in Chrome. I am completely blind! To add to the irritation, it took me a while to realize was that the javascript file was cached and changes would not be reflected in behaviour. I have put a random number into the script tag which fixes this issue. As I am debugging using netbeans connector in Chrome I have no idea why this does not force the js file to refresh. All in all, this appears to be a pretty shambolic toolchain.

call different function during page refresh and close javascript

I am creating a web app using web socket, which on user closes the tab I will make an API call to the server to clean the user related info in the server, I used onBeforeUnload listener in javascript, but this method also gets triggered during the page refresh.
I need to trigger a method only during the tab or browser close, but not during the page refresh.
I know this question has been asked several times, some solution suggested using cookies will not be helpful in my case
navigator.sendBeacon() method can be used for sending data from browser to server when a tab is closed.
Here is an example:
window.addEventListener("unload", function informServer() {
navigator.sendBeacon("/server-api-to-collect-data", my-data)
})
More information:
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
As far as I know, you can not listen to actions of browser's tab close or exits. For your application it is an "unload", whatever caused it...
The only thing I could think about is maybe add a listener for keyboard key press (F5), however it doesn't help in case someone refreshed by clicking on the browser's refresh button.
I don't know what is the use case, but most of the things should be done when a page unloads (no matter why) and/or when the page is back up again. So most of the solutions are for situations where your page is loaded again - and then you can determine what was the source of the load and make farther actions, but since you have an option were someone can close and never come back, that might not be the case.
Some solutions for page load up:
You can use Navigation type.
You can check referer.
You can use cookies or other types of browser storage.
I would recommend to rethink about your use case. Maybe you can do whatever you want on load up or leave it on onBeforeUnLoad without knowing the future :)

Stop or Disable browser Refresh button in javascript

I work in Ext JS. in Ext JS webdesktop application , when i click browser refresh button it reload and goes to login window. Because my first page is login. For this reason i need to stop this event or disable browser refresh button when this application launch. i can stop F5 but can't browser refresh button. So please some one help me for this issue.
Thanks...
No, wrong issue
You have to memorize the current View he is in. As far as i remember, Ext has mechanisms for that, otherwise you can still use a (session-)cooke.
and you don't have to start you application with the login-screen. It is just one view, and imo. the one you would want to use as few as possible.
So. on page load, check wether he is logged in, if no, show him the login-view, if yes check which view he was in and render it, otherwise send him to the start point.
this is basically the loop you have to send him through, on pageload, on refresh, onlogin, ...

disable javascript popup on refreshing page

I have written code like this to throw a pop up when my website is closed. But this throws the pop up even if i refresh my page. Please help me to throw popup only when i close the browser of that website. (disable popup on refreshing page)
<body onunload="javascript: exitpop()">
<script type="text/javascript">
function exitpop()
{
my_window= window.open ("","mywindow1","status=1,width=600,height=400");
my_window.document.write('<h1>My Team</h1><p>Thank you</p><p>If you accidentally closed website click here to go back to our website</p>');
}.
onUnLoad is called on a refresh because the browser is requesting a new page (well, the same page in the case of a refresh, but it is still a new call).
I am not sure if there is a way to check where the user is going next, but if there is you could compare it to their current url, if they are the same then the user is just refreshing and the script doesn't need to be run.
Good luck
Sadly, this is not possible. To the browser, it is essentially the same action.
However, if you just use a confirm() box, you can catch a user that is trying to leave without linking them back. If they are refreshing or if they really want to leave, hitting "OK" will allow that action to occur.
Let me know if you need this explained further.

redirect to a different page

I need some help here:
I am writing code to redirect a user to a different page and execute some code, if the user closes the window while he is working on something:
<script type="text/javascript">
window.onbeforeunload = function() {
location.assign('http://www.somesite.com');
return "go?";
}
</script>
Now the code works fine if the user presses cancel. But if he presses Ok, the windows closes without a redirect.
I just need a working code that does one simple thing - redirect the user if the window is closed - can anyone suggest me the code for it
I know that most of you will say that I should not be doing this and I know that this is not the best practice. But if any one of you has written a successful piece of code, please share it here.
Actually you should not. All the browsers make sure you don't.
Even if you find a way, rest assured that it will be on every browser's bug list, next day morning.
If the user wants to close the window there is nothing you can do.
He will.
The only thing you can do is ask politely "Are you sure?"

Categories

Resources