I've a simple HTML / Javascript page and I'd like to give to my user the ability to save the code of my current page that is a simple HTML / Javascript page.
I'm thinking something like right-click + "Save as"(Ctrl+S) + "Select All" (Ctrl+A) + Copy (Ctrl+C) + Paste (Ctrl-V) in some file locally
I'm using PHP ....
Any suggestion (or, better, example ..), will be appreciated ...
Cesaee
-
2How is the user going to access this data.. if they are already in a browser they can just hit ctrl + sDale– Dale2016年04月28日 14:31:40 +00:00Commented Apr 28, 2016 at 14:31
-
2Yes, ctrl+s already saves the html page as a local file.Jeremy Thille– Jeremy Thille2016年04月28日 14:39:00 +00:00Commented Apr 28, 2016 at 14:39
1 Answer 1
If it's static page which doesn't need user input or data, the easiest thing that comes to mind is linking to a ZIP file. You could link to a text file too so that it's easier for the user to copy and paste. But generally speaking, if you link to a resource the user's browser understands, the browser will try to render the content and make it a little trickier for the user to save.
Alternatively, as Al.G. pointed out below, you can set a header in PHP to download the webpage rather than render it. You can do this as follows:
<?php
header('Content-Disposition: attachment; filename="example.html"');
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
...
</style>
<script type="text/javascript">
...
</script>
</head>
<body>
<div id="body-wrap">
....
</div>
</body>
</html>
Whenever the user visits the PHP page, it will download all of the HTML, CSS, and JavaScript after the initial closing PHP tag. Perhaps you could link to the page they're viewing and say "download this page", which links to a copy with that PHP bit at the top?