I'm making a webpage wherein a user can make his/her own functions, then those functions will become like an option to the side of the webpage so when they are click/selected they can be used. Here's the flow
Here's a picture to further elaborate the problem:
First of all javascript can't save files on you computer (except you use ie). security,privacy...
If you want to let the user download his created file:
Chrome has the new download
attribute
but it works also on other brosers without the download attribute.
var txt='var x = 3,lol=fun;';
function dl(data,filename){
var b=document.createElement('a');
b.download=filename;
b.textContent=filename;
b.href='data:application/json;base64,'+
window.btoa(unescape(encodeURIComponent(data)));
return b
}
document.body.appendChild(dl(txt,'my.js'));
DEMO
in the comment is also a link for automatic download.
Want to save the data on your server:
note:if you want to save the function to the server you need a server language like php
<?php
if($_POST['fn']&&$_POST['data']){
$fh=fopen($_POST['fn'],'w') or die("can't open file");
if(fwrite($fh,$_POST['data'])){
echo '["ok":"file saved"]';// maybe return the saved code.
//echo json_encode($_POST);
}else{
echo '["error":"can\'t save the file"]';
}
fclose($fh);
}
?>
Want to save the data only on the users computer (using modern web technologies?)
you can also store the data inside (clientSide):
window.localStorage
window.sessionStorage
webSQL
indexedDB
Want to execute the script from it's string?
don't use eval();
use:
var js="var x='hey';alert(x);"
(new Function(js))()
To achieve what you need you need to mix this technologies.
if you have any other questions just ask.