Javascript saveas dialog
Asked Answered
W

2

0

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>
Wilscam answered 10/4, 2011 at 14:21 Comment(0)
T
3

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.

Ticking answered 10/4, 2011 at 14:25 Comment(2)
How can I make the donwload link look like a button?Wilscam
Just use some CSS styling. In Chrome you can inspect a button element, grab the CSS and pretty much copy it to your anchor tag :). @erikkallen: Great tip, thanks :DLichter
C
1
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.

Cauvery answered 10/4, 2011 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.