利用 document.execCommand 与 window.external 调用浏览器内置菜单功能

作者:窗边听雨时间:2026-10-27 15:56:25浏览:470

本文介绍了在网页开发中通过 JavaScript 调用浏览器内置菜单命令的方法。主要涉及使用 document.execCommand 执行打开、保存、打印、全选等操作,以及利用 window.external 对象实现查看源文件、安全选项、语言设置、收藏夹管理及刷新等功能。文章提供了完整的 HTML 按钮代码示例,展示了如何将这些功能集成到前端界面中,适用于需要模拟浏览器原生行为的场景。

调用浏览器内置菜单功能的代码实现

以下是实现相关功能的代码示例:

 
<input type="button" onclick="document.execCommand('open')" value=打开>
<input type="button" onclick="document.execCommand('saveas')" value=保存>
<input type="button" onclick="document.execCommand('print')" value=打印>
<input type="button" onclick="document.execCommand('selectall')" value=全选>
<input type="button" onclick="location.replace('view-source:'+location)" value=源文件>
<input type="button" onclick="window.external.ShowBrowserUI('PrivacySettings',null)" value=安全选项>
<input type="button" onclick="window.external.ShowBrowserUI('LanguageDialog', null)" value=语言设置>
<input type="button" onclick="window.external.AddFavorite(location.href, document.title)" value=加入收藏夹>
<input type="button" onclick="window.external.ShowBrowserUI('OrganizeFavorites', null)" value=整理收藏夹>
<input type="button" value=刷新 name=refresh onclick="window.location.reload()">
<input type="button" value=导入收藏夹 onclick=window.external.ImportExportFavorites(true,'');>
<input type="button" value=导出收藏夹 onclick=window.external.ImportExportFavorites(false,'');>
作者最新文章