I am creating a webpage with javascript that is doing some processing and building a data file in a variable. At the end of the processing, I want to provide a link that will let the user right click a link and do save as or something similar to save the results of that variable. Is there a way to do this without posting the results back to the server and writing out the file to the server and then redirecting to another page with the link. Basically I want to be able to provide the user with a way to do a save as or click a link to open the contents of a variable in javascript. I am thinking maybe an iframe and put the contents there?
Asked
Active
Viewed 116 times
2 Answers
1
You might be able to use a data://
URL for that, at least on some browsers.
Alternately, yeah, have the link open a new window and write the contents there. They can then "save as" the new page (live example):
function whenLinkClicked(event) {
var wnd = window.open();
wnd.document.write(/* ...contents here... */);
event = event || window.event;
if (event.preventDefault) {
event.preventDefault();
}
return false;
}

T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875
1
JavaScript doesn't have a great support for files until you get into up and coming file API. You could use a JavaScript and flash solution like Downloadify found here:

Declan Cook
- 6,066
- 2
- 35
- 52