javaScript将网页放到桌面,发送快捷方式到桌面

2018-05-03 02:27:37

不废话了,直接上代码,代码中有注释,有解释说明!

说明下:ActiveXObject只支持IE浏览器,所以放到桌面这个功能只支持IE浏览器,不支持火狐浏览器、谷歌浏览器等!


  1. <%@ page language='java' contentType='text/html; charset=utf-8'
  2. pageEncoding='utf-8'%>
  3. <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
  4. <html>
  5. <head>
  6. <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
  7. <title>放到桌面,发送快捷方式到桌面</title>
  8. <link rel='stylesheet' type='text/css' href='${pageContext.request.contextPath}/css/body.css'>
  9. <link rel='stylesheet' type='text/css' href='${pageContext.request.contextPath}/css/mark.css'>
  10. <link rel='stylesheet' type='text/css' href='${pageContext.request.contextPath}/css/console.css'>
  11. <script type='text/javascript' src='${pageContext.request.contextPath}/js/console.js'></script>
  12. </head>
  13. <body>
  14. <center>
  15. <h2>放到桌面,发送快捷方式到桌面(IE浏览器会显示放到桌面这个按钮,其他浏览器不会显示放到桌面这个按钮)</h2>
  16. <input id='toDesktopButton' type='button' value='放到桌面' οnclick='toDesktop(location.href, '雪豹软件工作室')'>
  17. </center>
  18. </body>
  19. <script type='text/javascript'>
  20. //将快捷方式发送到桌面
  21. function toDesktop(sUrl, sName) {
  22. try {
  23. var WshShell = new ActiveXObject('WScript.Shell');
  24. var oUrlLink = WshShell.CreateShortcut(WshShell
  25. .SpecialFolders('Desktop')
  26. + '\\' + sName + '.url');
  27. oUrlLink.TargetPath = sUrl;
  28. oUrlLink.Save();
  29. alert('成功创建桌面快捷方式!');
  30. } catch (e) {
  31. alert('当前IE安全级别不允许操作或您的浏览器不支持此功能!');
  32. }
  33. }
  34. //判断是否是IE浏览器
  35. function isIE() {
  36. if (!!window.ActiveXObject || 'ActiveXObject' in window) {
  37. // alert('是IE浏览器');
  38. return true;
  39. } else {
  40. // alert('不是IE浏览器');
  41. return false;
  42. }
  43. }
  44. //整个页面加载完之后执行函数
  45. window.onload = function() {
  46. var result = isIE();
  47. if (!result) {
  48. //不是IE浏览器
  49. //获得按钮元素
  50. var toDesktopButtonNode = document.getElementById('toDesktopButton');
  51. //隐藏按钮
  52. toDesktopButtonNode.style.display = 'none';
  53. }
  54. }
  55. </script>
  56. </html>
(0)

相关推荐