Wednesday 9 April 2008

Controlling the use of BACK and FORWARD on AJAX pages

Here is a wee trick that will have me branded the demon of scripting cos it is (i am told) very naughty to play with the action of BACK and FORWARD and REFRESH buttons.

This method works on both IE and FF I believe (well the modern ones).

Basically this came out of a need to stop users, who being users, will click BACK and FORWARD REFRESH with gay abandon usually at the most inopportune times, even more so when it as an AJAXy form.

Here is the code.. Bung it in your [head] Section
[script language='javascript']
window.beforeunload = isthatwise;
window.onunload = smug;
function smug(){
alert("OK you know best.. Bye-Bye Now!");
}
function isthatwise(evt){
return "Now listen you!\nThis is your programmer speaking\nI worked long and hard on this code\nand i think you maybe about to do something silly\nare you sure you want to do that?"
}
[/script]



I think it would probably be a good idea to change the text of the messages unless your users are as understanding of my foibles when it comes to error messages as mine are.

And then put this on your BODY tag

[body onBeforeUnload="return smugcomment();"]

When you get to a point on your form where unload is ok simply set the two events to null

window.beforeunload = null;
window.onunload = null;


It is also wise when AJAXing to ensure that there is no page cacheing so that they (the users) cant actually go back to a page in the middle of a transaction.

4 comments:

zoe said...

I'm completely confused.

Should geeky people understand that post? If so, that explains everything.

Steve McDonagh said...

@Zoe
OOOOOO !
I am tugging my forelock like mad and making "yes milady" noises ;)

Here am I a mere Twat (Geek version 1.1) and author of one of the blogs wot I read has left me a comment!

But to the point, yes that was a geek post, BIG TIME GEEKINESS! All about how to stop users making all your beautiful code go down the toilet when you create a web page that uses something called AJAX. All terribly excited to the sort of folk that normally read my blog.

I do try where possible to throw in the odd non-geek post. In fact some
in our geekshpere would say all my posts are a bit of the trival side.

Calling one of the bits of code in the example above isthatwise and the other smug is not generally considered good programming practice. But then I am part geek and part twat so i have an excuse.

Thanks again for leaving a comment :) i and SWMBO* have been reading you blog for quite some time and it always seems to bring a smile to wet Ireland.

Steve
* = She Who Must Be Obeyed

zoe said...

I thought AJAX was a cleaning powder. I'd better look through all the Twat's 'oh so boring' books on PHP (I actually use that one for thingies), CSS - and all the rest that I literally cannot see due to a large collection of vino bottles.

Not mine, oh No.

Did you know that I once did a 'geeky' (for me) post? Oh yes. About setting up a Flickr site - christ I was proud - but got "told off" by a geek who said he came to my blog to escape geekyness.

No pleasing anyone, is there?

Steve McDonagh said...

Zoe

LOL aye you cannae win. I have just been accused (perhaps with a big squidgey dollop of truth) of being a ASW because of some of my post titles are a bit "interesting".

ASW = Attention seeking Whore/Wanker (* delete as gender dictates) ASW is, I am led to believe, an official syndrome one that I am out and proud to have ;)
I am forming a support group with a a web site so that those of use who bend language until it squeals with delight have way of releasing our inner authors ;)

Disqus for Domi-No-Yes-Maybe