Re-Setting focus on RWA page

0
Morning All,   I need a little help with something that should be a quick easy task,   I have an app running on MSP 9.24.16 which requires the operator to scan into the app (scanbox widget) using a login card, all good so far.   We have now been asked to introduce a pop menu to allow operators to view other links such as Production Calendar etc as below,   The issue is when an option is selected from the popup menu the page opens in a new tab (that's good its what we want), the issue is returning to the main page the app no longer focuses on the scanbox unless the operator clicks on it or refreshes the page.   Is there a way of getting the app to reset the focus on the screen automatically as most operators wont know to click on the scanbox and we cant refresh the page as doing so would take the operator back to the home screen if they are in the middle of using the app. 
asked
1 answers
1

Hi Stephen,

You can ensure the scanbox input field regains focus automatically when the user returns to the tab by using a small JavaScript snippet. This script listens for the browser’s visibilitychange event and, when the tab is reactivated, sets focus back to the scanbox field after a short delay to ensure the UI has fully loaded. To implement this, add a JavaScript action in Mendix or include the script inside a custom widget. The code selects the scanbox input field and applies the focus() method whenever the tab is visible again. This way, users won’t need to manually click on the scanbox or refresh the page, improving the workflow by keeping the focus where it’s needed.

 

Dummy code:

document.addEventListener("visibilitychange", function () {
    if (!document.hidden) {
        setTimeout(() => {
            const scanbox = document.querySelector("input[name='Barcode']"); 
            if (scanbox) {
                scanbox.focus();
            }
        }, 200); 
});

Hope it helps!

answered