如何使用 jQuery / javascript 运行浏览器命令?
-
23-09-2019 - |
题
我有一页 HTML 格式,有两个按钮,保存和打印。
当用户单击打印时,它应打印页面,当用户单击“保存页面”时,应打开保存为...该页面的框。
首选 javascipt/jquery 解决方案。
谢谢。
解决方案
对于打印,您可以使用 window.print()
.
没有触发“保存”对话框的标准方法。在 IE 中你可以使用 document.execCommand('SaveAs')
.
编辑:从技术上来说 window.print
不属于任何标准(来源: 多维数据中心)但它广泛可用。
其他提示
尝试:(这只是为了“另存为”)从 这里
<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>
不隶属于 StackOverflow