harshpandya Posted May 30, 2008 Share Posted May 30, 2008 1) I need a help to store the Javascript variable in some file.2) I also need help to access that stored variable and use it later on in different files.Any help is appreciated ....Thanks, Link to comment Share on other sites More sharing options...
Ingolme Posted May 30, 2008 Share Posted May 30, 2008 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 More sharing options...
Jesdisciple Posted May 30, 2008 Share Posted May 30, 2008 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 More sharing options...
harshpandya Posted May 30, 2008 Author Share Posted May 30, 2008 I see now. I want to use PHP server side language and use Ajax for this. Yes i want to store this file on server. Please help...I could not find anything on Google. Thanks, Link to comment Share on other sites More sharing options...
Jesdisciple Posted May 30, 2008 Share Posted May 30, 2008 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 More sharing options...
harshpandya Posted June 5, 2008 Author Share Posted June 5, 2008 Where do you get this Examples from?Is there a good source anywhere?Let me know please Link to comment Share on other sites More sharing options...
Jesdisciple Posted June 5, 2008 Share Posted June 5, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.