March 19, 2012
Save as the part of a file
Question by user1278479
I would like have a button and when you click this button, un can save save as a part of the file.
For example if in DOM there is:
<div>
<div id="myButton">My Button</div>
<h1>My Title</h1>
<p>My Text</p>
</div>
Then when you click myButton the navigator opens a window for saving a file untitled “my button” with the content “My Text”. Is it possible or should i use a pop up or anything else to do that?
Due to the risk of hacking, i dont think it’s directly possible, but maybe a navigator plug in can do it ? Because i dont care for the support of cross-navigator, it’s only for a personal use.
Thanks !
Answer by Starx
You cannot do it with javascript alone, you need server-side script like PHP and then you have to send a ajax request to the server to page to save the text
Here is a scratch of what your code is going to look like
$("#myButton").click(function() {
//show some saving somewhere
$.post("yourserverpage.php",
{
//Send the data
title : $(this).parent().chidren("h1").html(),
text : $(this).parent().chidren("p").html(),
}, function(data) {
//sucess
// do something on success
}
);