How to run a browser command using jQuery / javascript?

I have one page in HTML, there are two buttons, saving and printing.

When the user clicks "Print", he must print the page and When the user clicks on the "Save" page, he must open the "Save as ..." for this page.

Recommended

javascipt / jquery.

Thank.

+3
source share
2 answers

For printing you can use window.print().

There is no standard way to start the Save dialog. In IE you can use document.execCommand('SaveAs').

EDIT: window.print - (: MDC), .

+2

try: ( " " ),

<html>
<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script >
$(document).ready(function(){
$('a#save').click(function() {
        if (!!window.ActiveXObject) {
            document.execCommand("SaveAs");
        } else if (!!window.netscape) {
            var r=document.createRange();
            r.setStartBefore($("head")[0]);
            var oscript=r.createContextualFragment('<script id="scriptid" type="application/x-javascript" src="chrome://global/content/contentAreaUtils.js"><\/script>');
            $('body').append(oscript);
            r=null;
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                saveDocument(document);
            } catch (e) {
                //no further notice as user explicitly denied the privilege
            } finally {
                //re-defined
               $("#scriptid").remove();
            }
        }
   return false;
    })
})
</script>
</head>
<body>
<a href="#" id="save">save the document</a>
</body>
</html>
+1

Source: https://habr.com/ru/post/1730174/


All Articles