How use Dmx.parse with dynamic variables

My script register the logout event for each user on system, but in this case, the variable “username_id” dont work. I dont know how the syntax work for this case, but if I switch the “user value” for a number, it works. Can someone help me? I need this variable working :frowning:

See Binding server connect variable inside Javascript

Thanks, but I dont understand how the parameter syntax works. In my example, I need to pass a dynamic value through the user parameter.

The “let” statement is simply setting the value of username_id to a string. You must also dmx.parse that or you just set a string

let username_id=dmx.parse(‘sc_get_user.data.query.id’);

The syntax for calling a sc is here from @sid:

(I am not familiar but maybe you should include the above code inside the addeventListener function in order to work…)

Please try both ways and let us know

1 Like

Unfortunaly, if I put the variable inside the function, it dont work. When I use a string like “2” on user’s parameter value, it works because is an ID from my database. The function works and returns on Log the username. But, if I use an variable, even like your example from @sid, dont works, I dont know why.

I posted the recommended way in the link above, but pulling it out to make it easier. It mentions you must wait until the data is loaded before accessing it. One way to do that is using ondone event on a server connect.

I’m curious why you are not using the Dynamic Events of ServerConnect to trigger the sc_log, instead of creating a script for it.

2 Likes

Its because I dont know how lol. This is my sc_log, is that right?

And if dont bother you, can you teach me how use the dynamic events for this case?

Maybe start from scratch and try to explain what is your goal?

Hi Teodor, nice to meet you. My goal is register login and logout from every user in my system. The system actually already do this, but in this case, Im trying to register the logout on “Log page” from every user that “closes the browser”. But actually the real problem is Im trying to use a javascript function that actives the server connect, that already works only with static values, like my IDs from database. But when I try to use a variable…well, it didnt work. I dont know how the syntax works in dmx.parse, like “How I put a variable here?” :face_with_raised_eyebrow:

You need to change your code to:

    <script>
        window.addEventListener("beforeunload", function(event){
            let username_id=dmx.parse("sc_get_user.data.query.id");
            dmx.parse("sc_log.load({user:"+ username_id + ", tipo: 'LOG', descricao: 'LOGOUT', campo: 'LOG/LOGOUT'})");
        });
                    
    </script>

also when showing code here, paste it and don’t upload screenshots of it. We can’t copy code from screenshots.

3 Likes

Thanks Teodor, It worked. Can you answer one more thing? I notice that this event “beforeunload” didnt works as I expected. Always when I reload the page, the script understands as “closing the browser”, and register logout on Log Page. I only want the script register when I close the browser. Did you already worked with this event? How do I fix that?

The beforeunload event triggers when the page is about to be unloaded, which occurs in various scenarios, including:

  1. Navigating to another page.
  2. Refreshing the current page.
  3. Closing the browser tab or the entire browser.
  4. Submitting a form.

This means that, by design, using beforeunload will capture all the scenarios mentioned above. If you only want to capture the event when the user closes the browser or the tab, it’s actually quite challenging due to the browser’s security and privacy implementations. There isn’t a native and full-proof way to distinguish between a page refresh and closing a tab/browser.

However, a workaround involves using the performance.navigation object. This object can tell you how the navigation to this page occurred, e.g., was it by a page reload or some other method.

Here’s a modified version of your code using this method:

window.addEventListener("beforeunload", function(event) {
    // Check if the page was reloaded
    if (performance.navigation.type !== performance.navigation.TYPE_RELOAD) {
        let username_id = dmx.parse("sc_get_user.data.query.id");
        dmx.parse("sc_log.load({user:" + username_id + ", tipo: 'LOG', descricao: 'LOGOUT', campo: 'LOG/LOGOUT'})");
    }
});

SOURCE: ChatGPT

3 Likes

Thanks for the reply kfwcett, but unfortunally this code didnt work. The log didnt regist when I reload, but when I closes the browser, it didnt regist neither. :slightly_frowning_face:

Yeah, it’s not going to be an easy process to accomplish. You might want to consider other options. Like triggering the sc_log when a user clicks the logout button/link in your app and not worrying about if they close their browser.

More from ChatGPT.

As mentioned earlier, distinguishing between a browser close and other navigation events is tricky.

The beforeunload event fires in many scenarios, so there isn’t a foolproof way to detect only a browser or tab close.

However, you can employ a combination of strategies to improve the accuracy:

  1. Leverage the Visibility API : One strategy is to utilize the document.visibilityState property. If a tab is not visible when the beforeunload event fires, it might indicate that the user has switched tabs or minimized the browser.
  2. Use sessionStorage : You can use sessionStorage to store a flag when the page is loaded. If the user refreshes the page, you can detect the presence of this flag and act accordingly.

Let’s attempt a solution combining the above:

window.addEventListener('load', function() {
    // Set a flag on page load
    sessionStorage.setItem('pageLoaded', 'true');
});

window.addEventListener("beforeunload", function(event) {
    if (!sessionStorage.getItem('pageLoaded')) {
        return;  // Do nothing if the page was refreshed
    }

    // Check if the tab is hidden
    if (document.visibilityState === 'hidden') {
        let username_id = dmx.parse("sc_get_user.data.query.id");
        dmx.parse("sc_log.load({user:" + username_id + ", tipo: 'LOG', descricao: 'LOGOUT', campo: 'LOG/LOGOUT'})");
    }

    // Clear the flag
    sessionStorage.removeItem('pageLoaded');
});

This solution attempts to detect a tab or browser close by:

  • Setting a flag in sessionStorage when the page loads.
  • Checking for the existence of this flag in the beforeunload event. If the flag exists, it’s the first time the event is fired since the page was loaded.
  • Using the visibilityState property to try and detect if the tab is hidden at the time the event fires.

This method still isn’t 100% accurate, as visibilityState can also change due to reasons other than closing the browser/tab (like switching tabs), but it can be a bit closer to what you want.

The challenge here is that browsers don’t provide a native event specifically for closing a tab or browser due to privacy concerns and potential misuse. Thus, developers often need to employ such workarounds to achieve their goals, with the understanding that it won’t be perfect.

1 Like

Thanks again for reply kfawcett, but I got the same result, didnt work. I think I will surrender this time, I think Im going too far only for a feature for the system. Im going to do other tasks, and maybe if I get another ideia about it, I will come back here to start a conversation with you guys again. Thanks again :slight_smile:

3 Likes