Javascript Stored Variable after page refresh - javascript

I'm having a problem relating to storing a variable within Javascript. I have a web monitoring page (I have written the code which works), which refreshes automatically every 5 mins.
If one of the sites I am monitoring goes down, the JS sends an email to the admin, but I only want to send this once.
My logic was to:
Check if a site is down
if variable emailPreviouslySent == 'no'
then send email to admin
write to variable emailPreviouslySent = 'yes'
Page would refresh
Check if a site is down
emailPreviouslySent == 'yes'
email would not be sent again
However I'm finding that the variable is emptied each time the page refreshes.
Is there a way of carrying the variable even though the page is refreshing, or perhaps another way around this?
Thanks for your help.

However I'm finding that the variable is emptied each time the page
refreshes.
You are inside state-less HTTP request. Store your variable in cookie to retain its value even after page refresh but notice that a cookie can be deleted by a user too.
Or as rightly suggested by #Matt Ball, a better approach would be to use HTML5's localStorage feature.

Related

Sharing username and password to the next HTML page

I need to share the username and password information to the right next HTML page after succeeding the login. Because the items in the second HTML page will appear according to the user identity and privilege.
I tried sharing the same js file between the 2 HTML pages. The first set the variables and the second get them, but they don't get passed. How do I do it? javascript? jquery? on the server side??
Thanks in advance :)
It can be done in many ways, but as the first language you mentioned is javascript, I will show you in it. So saving data across pages there are variable called session variable and the process of saving/retrieving them is called session management. There are many ways for session management, one most common way is using cookie. You can save the values in cookie, like this:
setCookie("key", "value", expire_time(integer));
And now on next page to get this value you can use:
var val = getCookie("key");
Hope this helps.
Username and password is a sensitive information you have to share it form Server side in these ways:
Use post method to share this information.
Set session on first page For user type and retrieve on very next page.
As your question says second page will appear according to the user identity and privilege. You can set user identity (User Type) and its privileges.

Passing a variable from one page to the next page

I am developing a Question & Answer website where a user is presented five puzzles on a page and the score is calculated using JavaScript as he attempts the questions on the page. The score is saved in a Javascript variable score. I have a paging system like this:
Now when the user clicks on 3 I want to send the variable score to the next page, where I have require scoreUpdateInDatabase.php on each such page such that the score of previous page is made permanent to the databse and the following PHP script presents him the next 5 questions.
How can I pass that score variable in secure way? I can't use GET because the user will modify it. I am satisfied with POST if this can be used, even though POST data can be modified but I just want minimal security.
P.S. Please do not suggest making AJAX call where in one side I will send score and while returning carries next 5 questions. I don't want to use AJAX to refresh content because it is not SEO friendly.
The simplest solution would be cookie based. Writing the value to a session cookie and the reading it.
You could use jquery cookie. It also gives you the option to require https if desired.
Save it in a session. POST would work equally well in this particular case but my preference would be storing it in the session.
The only secure way to do this is to pass the actual answers to the server using a POST or AJAX, do the calculation of the score also on server side and keep it in a SESSION variable.
More information on sessions in PHP
Try looking into Jquery - You should be able to return the value to the server scripting language (as you listed PHP as a tag, I assume you're using PHP). By using Jquery, you can get the javascript variable to the form BEFORE submitting the form to the next page.
Assuming you have used PHP to generate the form to submit initially rather than create the form in javascript. I would use Jquery - to get this file ( http://jquery.com/ ) and to include("jquery.js"); etc... in your PHP script for it to be used.
I would then convert the javascript variable(s) to a php variable and assign this to a hidden field in the form to be submitted to the next page using a $_POST[] variable.
However It will not be SEO friendly (POST and SESSION is not SEO friendly, but you should use them, continue reading)
We are talking of a game. No-one want that the Search engine index the last page of a game... because everyone can search on google (for example) for the last page of your game without playing.
You have to use ajax or post, but don't let google index every page of your game. It's nonsense.
Only the first page of your game should be indexed.

how to refresh a particular part of a php page?

I have a php page which displays the result of a particular solution submitted by the user. The page is displaying an image "running" while the solution is checked in the back end and when the solution checking is finished in the back end the result is stored in the database.Now as the result got stored in the database the same php page which was displaying an image "running" should display the image and result that it got from the database. So, it needs to refresh every time to fetch the result from the database.I have used an iframe for that part of the page and passed the solution id using SESSION to the page which iframe is using and that page is fetching the data from the database. but the problem is that due to refreshing when a different solution(with different solution id) is submitted in another tab of the browser then both the previous and the current tab in the browser shows the current page since the solution id is passed using the SESSION variable. I tried Ajax also but not getting desired result. I want that a particular tab on the browser display the result of the solution which was submitted on that tab only. How can i do this please someone help.I surfed the net but dint got any desired result.
Basically, your problem is, that
you allow the same session in two different browser tabs (or windows), so the session ID is not unique between solutions
you store the solution id in the session, so the session id must be unique between solutions
You can work around your problem by removin either of these two conditions - either do not allow session re-use (bad idea IMHO) or use the solution id rather than the session id in your AJAX call for the iFrame refresh, e.g. as a GET parameter

How to add javascript to page on login and logout in Drupal 6

I'm working on a module and am trying to add some javascript to the next page a user sees after logging in or out. Calling drupal_add_js() on hook_user (op == login) doesn't seem to work; I'm assuming this is because drupal_goto is called after the login is completed and a fresh page request is initiated.
I've considered using hook_user to set session variables which I can then respond to on the next page load but that seems somewhat fragile. Any suggestions?
If you want something to be carried over to a new page you only have a few options:
Alter the url.
Store in the database.
Store in session.
Altering the url, would probably be quite hard and messy. Storing in the session or database is basically the same thing. So you would probably want to use the Drupal session system instead of making your own.
You could add something in the session and then in hook_init check for it and if it's there add the js and delete it from the session.
I don't think you will find a much better solution, though it would be nice if there were.

Redirect to previous view and inject a javascript alert

I need to do the following:
I have a textbox, which appears in every page of the site, that allows to subscribe to a newsletter. This I've done already and the user is redirected to previous view after subscription.
I'd like to add a javascript alert to the page the user is returned to, something like "Thanks for subscribing". How can this be done?
Thanks in advance.
EDIT: Propably it's not clear from the post tags. I'm using ASP.NET MVC 2 Preview 1
If you are doing a HTTP redirect, then the page you will render needs to be passed some information so that it knows to include the javascript to open the alert box (adding an optional element to the page might be a nicer way to do this).
That information needs to be stored either in a browser cookie, or in a session store (which is keyed from a browser cookie). You can remove this once you've rendered your message, so that it is only shown the first time you visit that page after the redirect.

Categories