View Full Version : Writing/Downloading Element Content

09-24-2011, 06:38 PM

I have no clue on how to program the following using JavaScript: I want the user to be able to click a button or link in order to download the content of a <div id="query"> as a .sql file. I assume the variable for the writing to be document.getElementById('query').textContent (or perhaps .innerHTML) but I have absolutely no idea what the command for writing and downloading should be, nor have I been able to find any good tutorial/explenation on that using Google (yes I've tried myself first)

Thanks in advance,


Old Pedant
09-24-2011, 07:18 PM
Hmmm... JavaScript isn't really the right answer for this.

Much better to do this with server-side coding (PHP/ASP/JSP/etc.).

That way you can have the server-side script *tell* the browser to download the file, rather than just display it.

I think the best you could do with JS would be to copy the text to the clipboard (and now we are talking browser dependencies) so that you can then just paste it into a text file.

This *COULD* be done with MSIE, assuming that the browser user is willing to give special (dangerous!) permissions to the web site in question. Normally, you would only do this in an inTRAnet situation, not inTERnet.

09-24-2011, 07:46 PM
Right, well I'd like to do it in php, I prefer php over javascript (in terms of language structure) anyway, but the Javascript is responsable for writing in the <div> so I assume it's all on client side and there isn't an easy way to get it as a php variable in order to write it using php. Or do you know of one? A way to make php write content from a <div>?

09-24-2011, 09:06 PM
Could you use AJAX to send the contents of the div to the php script maybe? (this would be in key/pair format, or XML of course)

Old Pedant
09-25-2011, 02:23 AM
Don't think you need or want AJAX, actually.

You just want to send the <div> contents to PHP and then have PHP respond with a "download this file", where there's not really any file, just the <div> contents returned.

I'm not enough of a PHP person to know the right stuff to respond with. I could show you how it would work with ASP, but then you'd have to translate it.