Javascript | Odoo
Restricting previous page access in Browser
Not allowing the user to go to the preceding page

Hey everyone, recently I came across the problem of restricting the user from going back to the screen he came from and it took me quite some time to get it resolved đ˘ but finally with godâs grace and googleâs algorithm I figured it out đ. The problem was as follows.
Problem Statement: Donât allow the user to access the page he came from
So I created a form in Odoo and to restrict the user from accessing the previous page, I exploited the web features provided by jQuery.
Since the problem isnât specific to a framework I wonât be telling how to embed the code đŠâđť, Iâll just explain the javascript code snippet that would be used to restrict the user from going back.
Before we proceed further, I should mention that by default thereâs no such functionality provided by jquery which you can use directly because itâll affect the user's security, so being smartasses that we are weâll just use a hack to restrict the user from going back đ.
jQuery Code and Explanation
Code
$(document).ready(function () {
// using pushstate to add the page in history stack,
// the step is necessary or else popstate won't work
window.history.pushState("", "", window.location.path);
// whenver there is a change in active history the event gets trigerred
$(window).on('popstate', function(event) {
window.history.forward(1);
});
});
Line by line explanation
- All the code to restrict the user from going back to the previous page is enclosed in the jqueryâs document.ready method, the ready event occurs when the Document Object Model (DOM) is loaded. You can read more about it here.
- Inside the code, the first line is as follows:
window.history.pushState("", "", window.location.path);
In this line of code weâve made use of jqueryâs pushState feature, which helps us to add an entry to the browserâs session history, this step is necessary because we can only access the history that weâve added in the stack due to security restrictions by the browser you canât access the userâs personal browser history. You can read more about pushState here.
3. The last part is the part where we have made use of jQueryâs popstate feature.
$(window).on('popstate', function(event) {
window.history.forward(1);
});
This is the interface that gets fired when there is a change in the active history of the user session, since we just added something in the userâs active history in step two thatâs why weâre able to actually handle the event and take advantage of it. You can read more about it here.
Inside popstate we have added the following line.
window.history.forward(1);
What this line does is that it pushes the user forward by one page.
So in a nutshell, when the user presses the back button, heâs going to the previous page but before the page even is accessed or loaded we push the user forward, hence creating an illusion that the user is staying on the same page.
Conclusion: Due to browser security restrictions we canât actually suppress the back press functionality but we can make the user stay on the same page.