Javascript實現瀏覽器菜單命令

每當我們看到別人網頁上的打開、打印、前進、另存爲、後退、關閉本窗口、禁用右鍵等實現瀏覽器命令的鏈接,而自己苦於不能實現時,是不是感到很遺憾?是不是也想實現?如果能在網頁上能實現瀏覽器的命令,將是多麼有意思的事啊!下面我們就來看看如何用Javascript代碼實現瀏覽器菜單命令(以下代碼在Windows XP下的瀏覽器中調試通過)。

  一、【文件(F)】菜單中的命令的實現
  1、〖打開〗命令的實現
  [格式]:document.execCommand("open")
  [說明]這跟VB等編程設計中的webbrowser控件中的命令有些相似,大家也可依此琢磨琢磨。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=document.execCommand("open")>打開</a>
  2、〖使用 記事本 編輯〗命令的實現
  [格式]:location.replace("view-source:"+location)
  [說明]打開記事本,在記事本中顯示該網頁的源代碼。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=location.replace("view-source:"+location)>使用 記事本 編輯</a>
  3、〖另存爲〗命令的實現
  [格式]:document.execCommand("saveAs")
  [說明]將該網頁保存到本地盤的其它目錄!
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=document.execCommand("saveAs")>另存爲</a>
  4、〖打印〗命令的實現
  [格式]:document.execCommand("print")
  [說明]當然,你必須裝了打印機!
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=document.execCommand("print")>打印</a>
  5、〖關閉〗命令的實現
  [格式]:window.close();return false
  [說明]將關閉本窗口。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=window.close();return false)>關閉本窗口</a>


  二、【編輯(E)】菜單中的命令的實現
  〖全選〗命令的實現
  [格式]:document.execCommand("selectAll")
  [說明]將選種網頁中的全部內容!
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=document.execCommand("selectAll")>全選</a>


  三、【查看(V)】菜單中的命令的實現
  1、〖刷新〗命令的實現
  [格式]:location.reload() 或 history.go(0)
  [說明]瀏覽器重新打開本頁。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=location.reload()>刷新</a>
  或加入:<a href="#" οnclick=history.go(0)>刷新</a>
  2、〖源文件〗命令的實現
  [格式]:location.replace("view-source:"+location)
  [說明]查看該網頁的源代碼。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=location.replace("view-source:"+location)>查看源文件</a>
  3、〖全屏顯示〗命令的實現
  [格式]:window.open(document.location,"url","fullscreen")
  [說明]全屏顯示本頁。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=window.open(document.location,"url","fullscreen")>全屏顯示</a>


  四、【收藏(A)】菜單中的命令的實現
  1、〖添加到收藏夾〗命令的實現
  [格式]:window.external.AddFavorite("url", "“網站名”)
  [說明]將本頁添加到收藏夾。
  [舉例]在<body></body>之間加入:
  <a href="java script:window.external.AddFavorite("http://oh.jilinfarm.com", "胡明新的個人主頁")">添加到收藏夾</a>
  2、〖整理收藏夾〗命令的實現
  [格式]:window.external.showBrowserUI("OrganizeFavorites",null)
  [說明]打開整理收藏夾對話框。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=window.external.showBrowserUI("OrganizeFavorites",null)>整理收藏夾</a>


  五、【工具(T)】菜單中的命令的實現
  〖internet選項〗命令的實現
  [格式]:window.external.showBrowserUI("PrivacySettings",null)
  [說明]打開internet選項對話框。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=window.external.showBrowserUI("PrivacySettings",null)>internet選項</a>


  六、【工具欄】中的命令的實現
  1、〖前進〗命令的實現
  [格式]history.go(1) 或 history.forward()
  [說明]瀏覽器打開後一個頁面。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=history.go(1)>前進</a>
  或加入:<a href="#" οnclick=history.forward()>前進</a>
  2、〖後退〗命令的實現
  [格式]:history.go(-1) 或 history.back()
  [說明]瀏覽器返回上一個已瀏覽的頁面。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=history.go(-1)>後退</a>
  或加入:<a href="#" οnclick=history.back()>後退</a>
  3、〖刷新〗命令的實現
  [格式]:document.reload() 或 history.go(0)
  [說明]瀏覽器重新打開本頁。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=location.reload()>刷新</a>
  或加入:<a href="#" οnclick=history.go(0)>刷新</a>
  
  七、其它命令的實現
  〖定時關閉本窗口〗命令的實現
  [格式]:settimeout(window.close(),關閉的時間)
  [說明]將關閉本窗口。
  [舉例]在<body></body>之間加入:
  <a href="#" οnclick=settimeout(window.close(),3000)>3秒關閉本窗口</a>


  如果大家還整理出其他用Javascript實現的命令,不妨投稿來和大家分享。
  【附】爲了方便讀者,下面將列出所有實例代碼,你可以把它們放到一個html文件中,然後預覽效果。
  <a href="#" οnclick=document.execCommand("open")>打開</a><br>
  <a href="#" οnclick=location.replace("view-source:"+location)>使用 記事本 編輯</a><br>
  <a href="#" οnclick=document.execCommand("saveAs")>另存爲</a><br>
  <a href="#" οnclick=document.execCommand("print")>打印</a><br>
  <a href="#" οnclick=window.close();return false)>關閉本窗口</a><br>
  <a href="#" οnclick=document.execCommand("selectAll")>全選</a><br>
  <a href="#" οnclick=location.reload()>刷新</a> <a href="#" οnclick=history.go(0)>刷新</a><br>
  <a href="#" οnclick=location.replace("view-source:"+location)>查看源文件</a> <br>
  <a href="#" οnclick=window.open(document.location,"url","fullscreen")>全屏顯示</a> <br>
  <a href="java script:window.external.AddFavorite("http://homepage.yesky.com", "天極網頁陶吧")">添加到收藏夾</a> <br>
  <a href="#" οnclick=window.external.showBrowserUI("OrganizeFavorites",null)>整理收藏夾</a> <br>
  <a href="#" οnclick=window.external.showBrowserUI("PrivacySettings",null)>internet選項</a> <br>
  <a href="#" οnclick=history.go(1)>前進1</a> <a href="#" οnclick=history.forward()>前進2</a><br>
  <a href="#" οnclick=history.go(-1)>後退1</a> <a href="#" οnclick=history.back()>後退2</a><br>
  <a href="#" οnclick=settimeout(window.close(),3000)>3秒關閉本窗口</a><br>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章