FREE OutSystems Workshops - Build an OutSystems App in 1 hour | ENROLL NOW

Browser’s back button protection @OutSystems

< Back to Blog

Issue Explanation

You developed an OutSystems Web Application that needs to navigate to an external URL out of your control (e.g. OutSystems’ website), but afterwards the user decides to use the browser’s back button. What is the expected behavior? Will the browser re-render the page? Or should the preparation also be re-executed?

The behavior we observe is probably not what you’d expect: although the preparation of your web screen is not executed, the browser does re-render the page. In other words, the page is rendered with the information saved in the browser’s cache. Every time you make a request to the server using the Submit method the browser caches the response. This however does not happen if you use the Ajax Submit method. And what if proper display of your web screen requires the preparation to execute some business logic that affects what or how you show something to the user?

Proposed Solution

In order to run the preparation after coming back from an external URL (by clicking the back button on the browser) you need to force it to happen. This can be enforced for instance using HTML5 or JavaScript, but here we’re going to cover how to do it in OutSystems. I have found

several solutions for this issue using local or session variables but the only solution that is compatible with all browsers involves using a cookie to track when the user navigates away. Local variables are stored on the ViewState and some browsers when you navigate away they destroy the ViewState, this means when you come back from a different page, your local variables will be reset and you lose the information about if the user navigated away or not. Session variables are also a possible solution. To use them you have two possibilities: create a session variable for each page that you want to track, which is not reusable, or you can create a session variable what is a list, but this is not recommended. After analysing my options, I have decided to work with cookies as they can be stored until the session ends and created dynamically for each page.

How To Do It

● Create a button to navigate away, but be careful and select the submit method, because the cookie needs to be created, the destination must be a screen action, where you can create the cookie and navigate away using an External URL. To set the cookie you need to assign it a name, that can be whatever you want, for example you can use the GetBookmarkableURL function to make it unique for each page, and for the value use the same function.

● The OnRedirectNotify screen action is responsible to check the cookie , created when you navigated away, for the current screen and if the value matches with the current screen’s URL then it will reset the cookie to the default value, which is empty, force the preparation to run and render the page once again with all the conditions checked on the same preparation. Forcing it is as simple as ending the logic flow with the current screen as destination.

● The last but the most important step in this process is to drop an expression on canvas and set the escape content to no, then paste the JavaScript code below into it. JavaScript will be executed when the page is rendered by the browser and will fire the FakeNotifyWidget and consequently run the screen action that will force the browser to request the screen again from the server.

Turn Up Academy

Apply to our Turn Up Boot Camp

OutSystems Business Manager at LINKIT

Latest Articles

‍What’s new in OutSystems Developer Cloud

‍What’s new in OutSystems Developer Cloud

Have you heard about the OutSystems Developer Cloud and want to know more about what's new and what the big benefits are? Then this article is for you!

Tech Bites - Should we validate form data on server-side?

Should we validate form data on server-side

When including forms in applications, developers often wonder if they need to re-validate data on the server side after having checked it on the client. Why go to the trouble of checking the data twice?

Tech Bites
The Top 5 Mistakes Handling Asynchronous Data In Outsystems Reactive Apps And How To Avoid Them

The Top 5 Mistakes Handling Asynchronous Data In Outsystems Reactive Apps

When developing an OutSystems reactive web app, you would expect to display data quickly, but it doesn't always happen. Do you know why? We'll explore the reasons and how to tackle them.