很實用的 「設爲首頁」與「加入收藏」代碼

設置爲首頁的方法爲:
<a href=# onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('URL');event.returnValue=false;">設爲首頁</a>
URL就是你要設定的網址地址
要設置當前網頁的地址爲首頁,你能夠這樣:
<a href=# onClick="this.style.behavior='url(#default#homepage)';this.setHomePage(document.location.href);event.returnValue=false;">設爲首頁</a>
上述方法採用javascript的document.location對象的href屬性來得到當前網頁的全路徑。
同理,若是您只想將當前網頁所在的域名地址設爲首頁,您只需將document.location.href改成document.location.host便可。示例以下:
<a href=# onClick="this.style.behavior='url(#default#homepage)';this.setHomePage(document.location.host);event.returnValue=false;">設爲首頁</a>
說明:若是當前網頁的地址爲:http://www.design007.com/design/index.htm,則會將當前網頁的域名地址(www.design007.com)設爲首頁。

加入到收藏夾中的實現方法爲:
<a href="Javascript:window.external.addFavorite('http://www.design007.com','設計007')">加入收藏</a>
觸類旁通,與上面設爲首頁的方法同樣,若是您要將當前網頁加入收藏,而當前網頁的網址不肯定,您能夠採用如下方法:
<a href="Javascript:window.external.addFavorite(document.location.href,document.title)">加入收藏</a>
其中的document.title爲自動獲取當前網頁的標題的javascript代碼。
用到的代碼

document.location.host      //獲取網頁域名地址

document.location.href      //獲取網頁完整路徑

document.title                   //獲取網頁的標題javascript

相關文章
相關標籤/搜索