Login   Register  
Icontem

File: minimal.html

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Alessandro Vernassa (speleoalex)  >  JavaScript Save Page Form Offline  >  minimal.html  >  Download  
File: minimal.html
Role: Example script
Content type: text/plain
Description: minimal example
Class: JavaScript Save Page Form Offline
Save the current page as HTML to a file
Author: By
Last change: Have been reduced lines of code needed
Date: 2014-02-20 06:33
Size: 3,257 bytes
 

Contents

Class file image Download
<!DOCTYPE html>
<html><head>
        <title>Example minimal</title>
        <!-- start SaveOfflineForm code -->
        <script>
            (navigator.appVersion.indexOf("MSIE") != -1) && alert("If you use the Explorer browser will not be able to save the data. Use Chrome or Firefox!");
            //Add this code inside the tag "<body>":
            //<button type="button" onclick="SaveAs();">Save</button> 
            //save file------------------------------------------------->
            var DownloadFile = function(data, name) {
                var el = document.createElement('a');
                var ev = document.createEvent("MouseEvents");
                ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                el.setAttribute("href", 'data:application/octet-stream;base64,' + btoa(unescape(encodeURIComponent(data))));
                el.setAttribute("download", name || self.location.pathname.slice(self.location.pathname.lastIndexOf('/') + 1));
                el.dispatchEvent(ev);
            };
            var SaveAs = function () {
                var elements = document.getElementsByTagName("input");
                for (var i in elements) {
                    try {
                        elements[i].setAttribute("value", elements[i].value);
                        if (elements[i].checked)
                            elements[i].setAttribute("checked", "checked");
                        else
                            elements[i].removeAttribute("checked");
                    } catch (e) {
                    }
                }
                elements = document.getElementsByTagName("textarea");
                for (var i in elements) {
                    try {
                        elements[i].innerHTML = elements[i].value;
                    } catch (e) {
                    }
                }
                elements = document.getElementsByTagName("select");
                for (var i in elements) {
                    if (elements[i].options) {
                        var selectedIndex = elements[i].selectedIndex;
                        for (var o in elements[i].options) {
                            try {
                                elements[i].options[o].removeAttribute("selected");
                            } catch (e) {
                            }
                        }
                        elements[i].options[selectedIndex].setAttribute("selected", "selected");
                    }
                }
                var filename = document.getElementsByTagName("title")[0].innerHTML.replace(/ /g, "_") + "_" +
                        new Date().toISOString().slice(0, 19).replace('T', '_').substr(0, 10) +
                        ".html";
                DownloadFile("<!DOCTYPE html>\n<html>\n" + document.getElementsByTagName("html")[0].innerHTML + "\n</html>", filename);
            }
        </script>
        <!-- end SaveOfflineForm code -->
    </head>
    <body>
        
            Text:
            <input type="text" size="40" name="name" value="">
            <a href="#" onclick="SaveAs();">Save to Disk</a> 
    </body>
</html>