How to block back button (plain JS)


#1

I’m doing a project and I need to block back button in certain pages, can anybody help me?

regards!


#2

@NathanaelA wrotes a good tutorial on his site :slight_smile:

http://fluentreports.com/blog/?p=261


#3

Yesterday I found that page, but when I did that the app when run gives me a blue screen with errors =(

let me do it again to show the error.


#4

it doesn’t found the “iRefuseToGoBack” value…

function backEvent(args) {
if (iRefuseToGoBack) { args.cancel = true; }
}


#5

Yes, in the tutorial i didn’t found something for iRefuseToGoBack…

Maybe, @NathanaelA could help you :slight_smile:


#6

first on loaded or something, register the event
if (application.android) {
application.android.on(application.AndroidApplication.activityBackPressedEvent, backEvent);
}
then you disable it with
function backEvent(args) {
args.cancel = true;
}

then on then when navigating to the page that you dont want it disabled on you run this
exports.unloaded = function() {
if (application.android) {
application.android.off(application.AndroidApplication.activityBackPressedEvent, backEvent);
}
};


#7

On the pages you want to disable the back button for all u need to do is something like this global.iRefuseToGoBack = true once they are on that page and set it to false when they leave so then u can do something like

function backEvent(args) {
if (global.iRefuseToGoBack) { args.cancel = true; }
}

#8

excelent! this answer works perfectly!

thank you @michaelsowah