I have a ASP.net MVC project. i want to make a function in js that will execute only on the first load of this page in the current run of the program. In other words, if someone goes to a different page and comes back - i don't want it to happen again. but if i re-run the project from the beginning, i want it to happen.
i did my research - but everything i found - the ideas of Cookies and localstorage won't work for me, because they only work on the first load of the page of the user - so if we go out and rerun the project from the same computer, it won't work...
Anyone have an idea?
I am not sure you have clearly defined "first" "come back" and "current run." These are not straightfoward concepts in a web application, which from a logical perspective never starts nor stops, nor is there any concept of order of access unless you have specifically written a means of maintaining workflow state, which is not necessarily trivial.
If you want a script that will only run one time regardless how many times the page is accessed from any machine, you can set an application variable after it runs. On subsequent requests for the page, check for the application variable, and omit the Javascript if the variable is present. Note that the script will run again if the application resets for any reason, e.g. app pool recycle, which may be automatic depending on your web server settings.
If you want a script that will only run one time per browser instance, set a cookie after it runs. On subsequent requests for the page, check for the cookie, and if it is present, skip the execution of the script. Note that if a user has more than one browser, he will be able to get the script to run more than once, because most browsers do not share a cookie jar.
If you want a script that will only run one time per browser session (e.g. if you close and open the browser, the script should run again), set a session cookie after it runs. On subsequent requests for the page, check for the cookie, and if it is present, skip the execution of the script.
If you want a script that will only run one time per user, you will need some means of identifying the current user. So that means you need an authentication mechanism. In that case, only run the script if the user has just authenticated-- set a flag in the user database indicating the user has witnessed the execution of the script, and check it before rendering the script again.
You're partially there.
Set a cookie A on the server response with latest project start time.
If cookie B not already present, set cookie B on the client side with first load time and latest start time (from cookie A).
On the client side, on each load, check if cookie A == cookie b's latest start time. If not equal, run your function and update cookie B with latest start time.
Or like the comments said, just use a session and expire it when the project restarts.
Related
I am trying to realize booking system on my site. I want to allow user to book terminal entity for 10 minutes. Entirely, flow is enough big and goes over several pages.
While user navigate on this flow he should see how many time he has to complete operation.
Please advice me how to realize cross page timer.
I think the most straightforward way would be to create a cookie or an item in the user's localStorage which contains the start time of the transaction. Upon loading the page, you check for the existence of this value and compare it to the current time, if it exists.
You can use setTimeout or setInterval to run timers in the browser. However, a browser window is not guaranteed to continue to execute when it is not active. If the user hides the browser or goes to another application, the application cannot be sure that the timer is still running.
There is a newer specification for threading called web workers, which may help to alleviate the setTimeout problem.
https://developer.mozilla.org/en-US/docs/Web/API/Worker
As an alternative, I would suggest to store the start time on the server and continuously check the current time against it with polling HTTP requests. Or maybe just check it before they submit.
Or as stated by #sholanozie, store it in a browser cookie and just keep checking that.
I am building a WebApp (ERP) and I need to display the people currently logged in and active on the page. I managed to get something pretty accurate by listening on the mouse/keyboard events and periodically reporting to the DB.
I don't know how to mark people offline when they close the page. I tried using onbeforeunload, but it obviously fires when the user simply changes pages (click a link inside the ERP, that point to another page in the ERP).
I then tried to use WebSockets, but the problem is the same : everytime the page is realoded, the WebSockets connection is closed.
So I can think of two ways:
Use WebSockets indeed, and replace all links by a call to a javascript function that would somehow tell the server that the user is going to change page (so that the server doesn't mark it as offline). But that doesn't feel right, semantically speaking, links should be links, it simply points to another location.
Use either WebSockets or AJAX and never actually change page: links are replaces by a function that will call for the content, and display it on screen (updating the DOM with Javascript). But again, it doesn't feel right either, because semantically speaking the page would have no meaning and the URL would never change, so the user can't "copy paste" the link of the page to refer to it, right ?
So, is there a proper, clean way of doing this? Thanks for your help.
If each of your pages has a webSocket connection to your server, then on the server you can see when any given page is closed by seeing that the webSocket gets closed.
To avoid thinking that a user has left the site when they are just navigating from one page in your site to another, you simply need to add a delay server-side so that you only report that the user has left your site if there has been no webSocket connection from this user for some time period (probably at least a few seconds).
So, on your server when you detect that the last webSocket connection for this user has been closed, you set a timer for some number of seconds. If the user opens up another page on your site (either via navigation or just opens another page) before the timer goes off, you cancel the timer and count the user as still connected. If the timer goes off, then you now know that the user has been away from your site for whatever time period you picked (say 10 seconds) and to you, this will signify that they have left the site. You can control how long you want that time period to be before you decide that, yes they are gone.
All attempts at trying to "see" the user leaving your page IN the browser with Javascript are going to have holes in them because there are always ways for a web page to get closed without your client-side javascript having a chance to tell your server. The beauty of the webSocket solution is that the browser automatically and reliably tells your server when the page is now gone because it closes the webSocket and your server receives the notification that the socket has been closed.
As I understand you want to compute users active on website/pages.
Identify the user (99% unique id computed):
http://valve.github.io/blog/2013/07/14/anonymous-browser-fingerprinting/ you can use another library, there are few.
On each page send from time to time at page load meaning user is navigating or (60sec you can chose lower time frame meaning user is staing on the page) computed id (fingerprint js) to server (web-socket/ajax)
On server you need to have list of id's with expiration date (60s) increment when new user log's in (stored in database or session).
Retrieve on your website the count (60sec ajax/websocket) of id's having timestamp <= server time - let say 120sec.
Knowing if user is logged, and specify the page:
use an object to be sent at server {fingerprint: 123123124234, logged : true, page: home}
Clear your list if you are not storing in Database the users:
Separate thread (server only) access the object and destroy all nodes older then 10 min or whatever your page session is set.
js timer: http://www.w3schools.com/jsref/met_win_setinterval.asp
Let hope it's helpful, id did something similar using the timer at 5 min to sent to server if user is still on the page, or signal at page load.
Getting the cont of users in frame of 60 sec. And even the users with names present on page :)
Somebody already post this kind of question.
Hope this could help you .
Detect if user has closed ALL windows for a website?
I currently have a website that automates Instagram actions: http://instapromobiz.com. It is almost all javascript based, with some php to post to databases. Users login and an entry is created in a mySQL database, containing their username and how many credits they have (1 credit = 1 action). Then users add tags and press start, then the javascript makes requests via Instagrams API, and ajax+php is used to update their database.
The issue is that when the user leaves the page, or even refreshes it, the script will stop. Otherwise it will run forever.
I have a javascript file that contains all the functions needed to run the script until the user stops it.
My question is, can I use Google Apps Scripts to host this .js file so that when the user leaves the page the script continues to run. I've uploaded the code and published it, but I can't figure out how to access it from an external website.
node.js is out of the question, and I'd rather not convert the whole site to php(I don't know php well) and a cron wont work because of all the javascript.
Any help would be great, thanks!
Apps Script is going to be limited to a 5 minute execution time. You could create time based triggers to contend with that functionality to some extent, but the script will stop 5 minutes after being invoked.
If you still think Apps Script is a good fit, you would just need to deploy your script as a web app, and utilize a doGet(event) function or a doPost(event) function to receive the request from your external application. If you need to return content, there is ContentService to help facilitate that part of the process.
To maintain the different data points for each user, you will need to utilize ProertiesService.getUserProperties(), to store persistent string values for each user.
The other part of Apps Script that may come into play with your Javascript, is that Apps Script uses caja sanitization for javascript (just something to be mindful of, if you run into issues).
If you MUST defeat the execution time problem in google apps script. You can monitor the execution time and at a certain point before time expires invoke the script recursively and pass it the parameter of wherever it left off.
I'm coding a translation system for multiple messages.
I've a page with a table listing all the messages that need to be translated. When a translator click on a message to translate it, I lock it cause no other translator can be assigned to it.
The problem arises when a translator enters and instead to write something, leaves the page in an unconventional way like back button or closing the browser.
If this happens I want to unlock the message to make it available again to other translators.
How can I reach this behavior?
I know the javascript onbeforeunload event but triggers also every time a user refresh the page and this isn't what I want.
ty in advance
EDIT:
seems that implementing a js ajax call to notify every minute the server is the way to go. Now the question is how to handle the PHP server side?
You can use WebSockets, but in my opinion its a immature technology and still away from being firewall friendly.1
So, you can use HTTP polling. Use a JavaScript to make a HTTP request to the server from time to time, so it will show to the server the client is still in the page. The time will depend on connection, number of user's etc, but putting it before the keep-alive expires is a good idea as the TCP/IP connection is still open.
If the user leaves the page, the polling will not execute, and after not receiving the HTTP request for x seconds plus some time, the server can assume the user is not more on the page.
1 - Well its the firewall that is not friendly of WebSockets, this probably will change with time
What's wrong with using onbeforunload and asking the person if they really want to leave the page with unsaved changes?
I'd use the mentioned approach while proving a manual save button.
Also, a timeout on locks would help, so if the person has not edited the field for several minutes, it'd be unlocked and the person would be notified via a JS AJAX call.
EDIT: to implement the AJAX timer, I'd do the following:
save last access time to the translation item in a database or in a file on the PHP side
once every 30 seconds, do an AJAX call to a PHP script that will verify the last access time
depending on the result, return an "OK" or "TIMEOUT" message from the PHP script to the JavaScript part, which will then either do nothing (for OK) or deactivate the translation and inform the translation about the timeout
I want something like this : first time my web page is loaded, I store a function fn in browser's local storage. From then on, whenever a request to my domain is to be made fn is executed before even making that request (but of course after figuring out that the request is to be made to my domain)
Is it possible?
What I think you are looking for is called Application Cache. Application Cache allows you to specify a cache manifest to tell a supported browser that certain files on your site should be cached, and automatically loaded form cache the moment the user visits your site a second time. The browser will check for updates AFTER the site is loaded, so even if you update the manifest file (which is the only way to tell a browser one of the cached files changed), the browser will download the updated files in the background. Then you can choose to reload the page automatically or just let the user use the old cache until the next time they load a cached page.
http://www.html5rocks.com/en/tutorials/appcache/beginner/
This is the ONLY way to load html/images/js or execute js before a single request is sent to your site. This also means if you design your site/app correctly, it can be used even if the user is completely offline.
I hope not since those functions would be the net equivalent of land-mines. Imagine I know a bank merger is going to happen so I buy a bunch of domains that the post-merger company might have, and do something to get traffic to go there which stores one of these functions in the browser cache. The merged bank then comes after me for domain-squatting and I hand over the domain without much of a fight. The script I embedded now runs for any request to the bank even though I don't own the domain, so it can launch a pop-under for my phishing site only when the user actually navigates to the banks website.
You cannot store functions using any of the semi-permanent means of Javascript (local storage/cookies). You could store a function name, which you would then use to determine what function to execute. If you include the javascript file on every page on your domain, then it's possible.
This could be done quite easily by setting a cookie, or storing a value in local storage. If the value doesn't exist, it's the first time.