設為首頁加入收藏相容360/Firefox/Google/IE等主流瀏覽器的代碼

來源:互聯網
上載者:User

不用找了,我試過好多次,ie,Firefox,Google瀏覽器此代碼都是無法復原相容。
下面是我的簡單解決方案。想把這個問題完全解決,方法就是像其他主流網站一樣專門建立添加為首頁的操作步驟協助頁。

js代碼:
複製代碼 代碼如下:
<script type="text/javascript">
// 設定為首頁
function SetHome(obj,vrl){
try{
obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
}
catch(e){
if(window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e) {
alert("此操作被瀏覽器拒絕!\n請在瀏覽器地址欄輸入“about:config”並斷行符號\n然後將 [signed.applets.codebase_principal_support]的值設定為'true',雙擊即可。");
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage',vrl);
}else{
alert("您的瀏覽器不支援,請按照下面步驟操作:1.開啟瀏覽器設定。2.點擊設定網頁。3.輸入:"+vrl+"點擊確定。");
}
}
}
// 加入收藏 相容360和IE6
function shoucang(sTitle,sURL)
{
try
{
window.external.addFavorite(sURL, sTitle);
}
catch (e)
{
try
{
window.sidebar.addPanel(sTitle, sURL, "");
}
catch (e)
{
alert("加入收藏失敗,請使用Ctrl+D進行添加");
}
}
}
</script>

調用代碼:
複製代碼 代碼如下:
      <li class="fore1"><a href="javascript:void(0)" onclick="SetHome(this,window.location)">設為首頁</a></li>
<li class="ge">|</li>
<li class="fore2"><a href="javascript:void(0)" onclick="shoucang(document.title,window.location)">加入收藏</a></li>
<li class="ge">|</li>

名種樣式的加入收藏和設為首頁代碼
還有一些最基本的html代碼,只供初學者參考。
注意:此代碼只相容IE瀏覽器
1:實現"
加入收藏"功能的代碼如下:
複製代碼 代碼如下:
<a href="javascript:window.external.AddFavorite(‘http://www.baidu.com‘,‘百度‘)">加入收藏</a>

你只需將連結與站名改為你自己的站名即可.
如果你要實現滑鼠划過時即提示是否加入收藏,只需將代碼改為:
複製代碼 代碼如下:
<a onmouseover="window.external.addFavorite(‘http://www.baidu.com‘,‘百度‘)" target=_self href= >加入收藏</a>

----------------------------------
2.設為首頁代碼(html源碼)
複製代碼 代碼如下:
<a style="cursor:hand" onclick="this.style.behavior=‘url(#default#homepage)‘;
this.setHomePage(‘http://www.baidu.com/‘);">
<img width=16 height=16 border=0 src="images/house.png">
<u> Set as HomePage</u></a>

1.文字型:
複製代碼 代碼如下:
< a onclick="this.style.behavior=‘url(#default#homepage)‘;this.setHomePage(‘http://www.baidu.com/‘);" href="#">設為首頁</a>

範例:
設為首頁
2.按鈕型:
複製代碼 代碼如下:
< input TYPE="button" VALUE=" 設為首頁 "onclick="this.style.behavior=‘url (#default#homepage)‘;this.setHomePage(‘http://www.baidu.com/‘);" href="#">
離開時自動提示設為首頁
<body onunload="BASEBody.style.behavior=‘url(#default#homepage)‘;if(!(BASEBody.isHomePage
(‘http://www.baidu.com/‘)))BASEBody.setHomePage(www.baidu.com//‘);">

—————
———————————————-
開啟頁面時自動快顯視窗詢問是否設為首頁
將以下代碼放在<head></head>之間:
複製代碼 代碼如下:
<script
language="javascript">
function myhomepage(){
this.homepage.style.behavior='url(#default#homepage)';this.homepage.sethomepage(‘http://www.baidu.com');
}
</script>
<p align="center"><a href="http://www.baidu.com/" name="homepage" onclick="myhomepage();"></a>

再將下面代碼加入<body>內:
onload="myhomepage();"
即:<body onload="myhomepage();">
——————————————————————–
滑鼠指向時提示設為首頁
將下列代碼插入<body>區中:
複製代碼 代碼如下:
<A href="http://www.baidu.com/" onmouseover ="this.style.behavior='url(#default#homepage)';this.setHomePage(‘http://www.baidu.com/'); " target="_blank">設為首頁</A>

—————————————–
關閉當前視窗的功能
這裡我們可以先輸入用來標示的文字“關閉視窗",用滑鼠拖動選中它,在“Link"輸入框中鍵入“/",同時切入源代
碼視窗,在連結代碼中鍵入該事件-onclick="javascript:window.close(); return false;"。
完整的代碼為:< a href="/"onclick="javascript:window.close(); return false;">關閉視窗< /a>
----------------------------------
3.聯絡站長的話實際上是連結到一個電子郵箱地址:
複製代碼 代碼如下:
<a href="mailto:baidujishubu@163.com">電子信箱</a>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.