---
title: "很实用的 “设为首页”与“加入收藏”代码"
author: "jianghe_03"
date: "2008-06-27T14:36:02Z"
tags: []
slug: "untitled"
draft: true
lastmod: "2008-06-27T14:36:02Z"
id: 208644
url: "https://imthinking.iteye.com/blog/208644"
---

设置为首页的方法为： 
<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                   //获取网页的标题