0

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

2
  • 2
    How is the user going to access this data.. if they are already in a browser they can just hit ctrl + s Commented Apr 28, 2016 at 14:31
  • 2
    Yes, ctrl+s already saves the html page as a local file. Commented Apr 28, 2016 at 14:39

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

It's not difficult at all - just send a Content-disposition header.
Thanks, I wasn't aware of that. My answer is updated to reflect that useful tidbit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.