How to run Browser command using jQuery / javascri

2019-07-07 06:25发布

问题:

I have one page in HTML , there are two buttons , save and print.

When user click on the Print it should print the page and When user click on the Save page it should Open Save as... Box for that page.

javascipt/jquery solution preferred.

Thanks.

回答1:

For printing you can use window.print().

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

EDIT: Technically window.print isn't part of any standard (Source: MDC) but it's widely available.



回答2:

try: (this is just for "save as") ripped and edited from here

<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>