Detect browser back or forward button clicked

4,064 views
Skip to first unread message

Anita

unread,
Jan 7, 2010, 7:34:36 AM1/7/10
to Google Web Toolkit
Hi,

How can I detect on GWT client side that browser's back or forward
button is clicked? Because on click of back and forward button i want
to perform custom operation.

Thank You.

Tercio

unread,
Jan 7, 2010, 8:19:27 AM1/7/10
to Google Web Toolkit
You can't.

What you can is handle history changes, that occurs when user push
back and forward in the browser, but only works for links in the same
page(#anchor).

public class Dummy implements ValueChangeHandler<String>
{
public Dummy()
{
// Register ourselves with the History API.
History.addValueChangeHandler(this);
}

public void onValueChange(ValueChangeEvent<String> event)
{
// User has clicked back or forward, the event parameter has the
value itself.
}
}

Regards,

Anita

unread,
Jan 7, 2010, 8:32:10 AM1/7/10
to Google Web Toolkit
Hi Tercio,

thanks for the quick reply. Is it possible that i can get it from DOM
or from javascript native code.

Rogério Valente

unread,
Jan 7, 2010, 9:00:21 AM1/7/10
to Google Web Toolkit
I don't know if this can help you but, we just add a script in
<appName>.html to disable some functions key (F5 to refresh).
Take a look at the code below. Put it in a javascript file in
<appName>/war/js and add a script tag on the head section of your app
main html file ..
something like <script type="text/javascript" language="javascript"
src="js/disableFunctionKey.js"></script>

Maybe you can find a solution to disable the buttons.

[CODE]

//Disable F5 - Refresh
var isIE = ( navigator.userAgent.toLowerCase().indexOf("msie") !=
-1 );
function interceptKeyDown(e) {
keyCode = e.keyCode;
//F5
if ( keyCode == 116 ) {
if(isIE) {
// IE only
e.keyCode = 505;
}
return false;
}
}
function interceptKeyPress(e) {
if( !e ) {
if (window.event)
e = window.event;
else
return;
}
//NS 4, NS 6+, Mozilla 0.9+, Opera
if(typeof(e.which) == 'number') {
var keyCode = e.keyCode ? e.keyCode : e.which ? e.which :
void 0;
if(e.charCode == null || e.charCode == 0 ) {
// F5
if ( keyCode == 116 ) {
e.stopPropagation();
e.preventDefault();
}
}
}
}
function attachEventListener( obj, type, func, capture ) {
if(window.addEventListener) {
//Mozilla, Netscape, Firefox
obj.addEventListener( type, func, capture );
} else {
//IE
obj.attachEvent( 'on' + type, func );
}
}

attachEventListener(document,"keydown",interceptKeyDown,true);
attachEventListener(document,"keypress",interceptKeyPress,true);

[END-CODE]

Anita

unread,
Jan 7, 2010, 9:13:14 AM1/7/10
to Google Web Toolkit
Hi Rogério Valente,

Thanks for the reply.
If i disable the buttons how will i come to know that the back button
is clicked ?

Rogério Valente

unread,
Jan 8, 2010, 8:52:07 AM1/8/10
to Google Web Toolkit
Well, in fact you don't "disable" the buttons.
In the disableFunctionKey.js file, the function just intercept the
keydown or keypress
and return a value tha is not the expected by the browser.

As I wrote, maybe you can find a solution that intercept the browser
back button click.

Reply all
Reply to author
Forward
0 new messages