How to run Browser command using jQuery / javascript?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 21:17:41

问题


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>


来源:https://stackoverflow.com/questions/2145538/how-to-run-browser-command-using-jquery-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!