Reputation: 2562
Is there a way for javascript to access a variable left by the user of a specific computer? I know that system variables are out of the question.
Currently I have a POS web-app that sends HTTP HEAD requests to the server specifically for the purpose of updating the pos terminal's display pole unit.
The app:
1) Sends HEAD request to the server
2) The server receives the request with the payload and writes to a virtual port which is connected to the pos terminal's LAN > COM port which in turn writes to the pole
However when other devices like mobile phones and laptops use the pos app, all of them are sending these requests every time a line item is changed in the form. That's a lot of useless requests (because these computers don't have their own display pole).
I'd like it so that the JS can search for a specific variable that the user can put in their system. If it's there, send the request. If not, don't even try.
Something along the lines of a browser addon maybe? As in "for this domain, set this variable".
Upvotes: 0
Views: 48
Reputation: 707158
For security reasons, plain Javascript in a web browser cannot access things outside of the browser environment. A browser add-on can access things outside of the browser so it could create an interface for some outside setting.
It sounds like what might make more sense is for the pos terminal to specify a slightly different URL when it uses the POS web app and that different URL (say a query parameter) would tell your app server to place a Javascript variable in the page indicating that this use of the app does have a display pole unit attached so it should do whatever one would do when that is connected.
Meanwhile, the publicly known URL that is used from a laptop or a mobile phone would not have that special parameter in the URL and thus the Javascript in the page could know that it is not connected to a display pole unit and should act accordingly.
You can have the identical web app for the two classes of users with this one configuration variable set differently based on what URL initiated the session.
Other places you can store configuration info in the browser are cookies and LocalStorage. So, the setup process for the pos terminal could cause a LocalStorage value to get set such that your Javascript could tell from that LocalStorage value that this browser is connected to a display pole unit, but the mobile phone/laptop browsers would not have that LocalStorage value set so the Javascript could act accordingly when they were using the app.
Here's another idea. I don't know what kind of browser is in your POS terminal, but if it has a unique user agent string or it can be configured to have a unique user agent string, then your Javascript in the page could check navigator.userAgent
and examine it to see if any particular markers were present in the string that indicates this is the POS terminal browser, not some other browser.
Install a browser custom browser plug-in (that doesn't have to do anything except be present with a known name) in the POS terminal browser. Then, use Javascript to detect whether that plug-in is present. If so, you know to act like it is connected to a display. If not present, then no display attached.
To summarize the options:
Use a query parameter on the URL when accessing the webapp from the pos terminal. This would tell your Javascript that there is a terminal attached. You could also then set a cookie whenever you see the query parameter such that other pages (without the query parameter, but loaded from the same device) would see the configuration too.
As part of the setup process for the pos terminal, run a page that would set a cookie to tell the JS that this viewer is the pos terminal.
As part of the setup process for the pos terminal, run a page that would set a value into LocalStorage to tell the JS that this viewer is the pos terminal.
Add some custom indicator to the user agent string in the POS terminal browser that your Javascript can then detect.
Install a custom browser plug-in on the POS terminal which your page Javascript can detect the presence of and act accordingly.
Upvotes: 2