what happens to javascript variable after a call to the server? - javascript

I just began to learn javascript, so here is a silly question :
What happens to a javascript variable after a call to the server? Are all the variables wiped out?
I read somewhere that javascript variable in ajax can act like session or cookie. Is that true?

All of the run-time state is reset whenever the browser does a page-load, such as navigating from foo.com/bar to foo.com/baz. This includes all JavaScript variables, as well as the current DOM. However, asynchronous calls to the server, such as XHR, do not affect run-time state, and all JavaScript variables will stay.
If you'd like to preserve values between page-loads, you can use cookies or localStorage.

It depends, what scope the variable is in. Also, Ajax is different then submitting a page, so your variables are persisted.

Related

JavaScript – Security overwriting variable by user

I have a variable in JavaScript:
var userIp = '192.168.0.1';
However, user can open browser console and overwrite it:
userIp = '123.45.127.21';
How I can lock this variable, to user can't change it value? Is it possible?
How I can lock this variable, to user can't change it value? Is it possible?
No, it isn't. You can make it harder by making the variable not a global, but it's still not remotely hard.
Client-side code is completely and totally insecure. Users can change values of variables, modify code, completely replace the code, etc. They can also manipulate the page contents. Anything the client side sends to the server may be spoofed, and so the server has to treat everything it receives as potentially-compromised.

Pass PHP class instance through AJAX?

I recently wrote a PHP wrapper class for a new API we are using and have been asked to setup a demo which makes use of it. Certain features can be called directly from PHP, however things such as performing actions on button clicks requires I make use of JS/AJAX.
As I already have an instance of the object in my main PHP file, can I pass this as a parameter to JS and then pass it through Ajax to my handler or is it necessary to establish two separate instances?
The closest thing to doing what you want that I can think of would be copying the PHP object to a JavaScript variable via json_encode and then passing that variable back to your PHP code during the AJAX event via the data parameter. PHP code doesn't persist in the way that you seem to be describing - once a page has been requested by a browser, your PHP code for that page is done, there are no variables persisted by the server after that point.

How to use saved variables saved in Javascript file after moving to new HTML page?

I have a javascript function where in one part, I perform an AJAX call and store some information.
In the same function, I move to a new HTML file.
window.location.href = "deals.html";
Once moving to the new deals.html file, how can I access the information I stored earlier?
Thank you for your help.
The information you stored using AJAX function resides in server now, you have to initiate another AJAX function to retrieve that information from the server when you get the new page.
You can write a javascript function to keep the data using cookies and access it in next page. This will be executed just befor redirecting to another page.
There is something called localStorage which is supported by latest browsers, you can use that to store response from first call, and use it second page.
This can only be achieved if you use cookies in JavaScript because, as you are navigating to another html document you loose the status of all the variables in the already executed script.
So before navigating away to another page set as many cookies as required.
References on cookies

Making a Javascript variable remain same in all pages

I am using an external javascript file in two pages. I change the value of a variable from the first page. Now when I use the second page, the value of that variable goes back to its initialisation value. I want to maintain the value of the variable. How can I do this?
var mail="not yet added";
I change this value in page 1 to "xxxxxx#xxxx.com".
Now when I access this in page 2 I get "not yet added"
You can make use of the windows.name object as long as you're in the same windows or tab. Have look here.
Otherwise the persists.js framework offers a number of different ways to persist information and share it between pages etc.
Your final option is of course using a basic cookie.
Write it to a cookie on javascript, otherwise post the value back to the server (i.e. php or whatever you are using) - then you can print the variable explicitly on the next JS page if thats easier.
You can also pass variable to second page by adding #hash to link then in second page just retrieve it and update the variable with hash value.

How to maintain state of global vaiable in JavaScript?

Hi I have script below: Basically I have a navigation anchors "next" and "prev" which increments or decrements currentDate to navigate between days. This means variable currentDate cannot refresh once the page is load. This variable should be like static variable which should maintain its state, however when ajax call comes back this variable is updated each time to new Date().
<script>
var currentDate = new Date();
$(document).ready(function() {
//ajax call to retrieve some values from the server
});
</script>
Is it possible to declare a global variable which doesn't refresh with each ajax call? Or is it better to get the updated date from the server in this situation? Or am I handling this situation wrongly?
Thanks!
The global javascript variable would not normally reset to "new Date()" simply because an AJAX call returns. It would reset if the page is refreshed. Are you sure there isn't a windows.location call or something else in your AJAX return handler that refreshes the page?
currentDate will not change with each ajax call. If it does, then either it's being set somewhere else or the page itself is being refreshed.
State is not maintained between browser reloads. But there are a bunch of possibilites to persist your data, though, and read them on page load again. You could store the data:
using JavaScript local storage (only for HTML5)
by writing to and reading from cookies (like most websites do)
by using the windows pageName as a data store. This is, in my opinion, the most creepy solution :-)
by using a server to persist the stuff (like applications like Gmail do)
On each of these topics plenty of documentation is available out there.

Categories

Resources