Delashmate
Delashmate

Reputation: 2424

Javascript saveas dialog

I am trying to write save as dialog with javascript,

I have a content of data, and I want to allow the user to save it,

I managed to get the code below to work, but this code is changing the html data,

So my question is:

1)How can I retrived the html data back, as it was before I the click on the button?

2)Can I do it more elegant way?

<script type="text/javascript">
function saveChanges()
{

var oldHtml = document.documentElement;
document.open("text/html","replace");
document.write("Hello");
document.close();
document.execCommand("saveas", false, "default.htm");
}
</script>

<body>
<button onclick="saveChanges();">Click to save123</Button>
</body>

Upvotes: 0

Views: 5820

Answers (2)

jazgot
jazgot

Reputation: 2063

document.execCommand('SaveAs'...)

is not part of standard, and is not supported by all browsers. Better way to do this is to provide download link.

Upvotes: 1

erikkallen
erikkallen

Reputation: 34391

The usual way to do it is to provide a download link which, when clicked, makes the server return a result with the Content-Disposition: attachment header set.

Upvotes: 3

Related Questions