Jump to content

Store Variables in Some file


harshpandya

Recommended Posts

Javascript cannot write in files, and has a few limitations when reading them. Javascript is a browser scripting language, it doesn't have a lot of control because it runs on the client side.

Link to comment
Share on other sites

Where do you want to put this file - on the client machine, on your server, ...? On the client, it's a cookie and JavaScript can manage that on its own. On your server, you need AJAX + some server-side language to write it and a simple AJAX call to retrieve it.Assuming you want to put it on your server, this is only one thing, right? If so, a simple TXT file would probably be enough. How to write the file would depend on which server language you use.

Link to comment
Share on other sites

These are untested...saveVariable.php

<html>	<head>		<script type="text/javascript">			function getAjax(){				var ajax;				try{					// Firefox, Opera 8.0+, Safari					ajax = new XMLHttpRequest();				}catch (e){					// Internet Explorer					try{						ajax = new ActiveXObject('Msxml2.XMLHTTP');					}catch (e){						try{							ajax = new ActiveXObject('Microsoft.XMLHTTP');						}catch (e){							alert('Your browser does not support AJAX!');							return false;						}					}				}				return ajax;			}			window.onload = function(){				var ajax = getAjax();								//write				ajax.onreadystatechange = function(){					if(ajax.readyState == 4){						alert(ajax.responseText);					}				}				ajax.open('POST', 'saveVariable.php', true);				ajax.send('variable=BLAH!');								//read				ajax.onreadystatechange = function(){					if(ajax.readyState == 4){						alert('variable = ' + ajax.responseText);					}				}				ajax.open('GET', 'variable.txt', true);				ajax.send(null);			}		</script>	</head></html>

Link to comment
Share on other sites

I'm glad you asked; I should have given help in the opposite order, but I didn't.The AJAX stuff is here (although I used different identifiers): http://w3schools.com/ajax/default.aspEDIT: But it doesn't note the difference between POST and GET. POST is for sending data; GET is for getting them. With POST the data are sent in the send() function; with GET the desired data are, if necessary, indicated in the URL's query string. (Not that it matters much for AJAX that I can see, but that's how the HTTP standard set it and I don't know all the potential effects of not following it.)The PHP stuff is here:http://php.net/fwritehttp://php.net/fopenAnd of course, I'll explain the 'glue' in case you don't understand it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...