在html中怎么用 js收藏网页到收藏夹

JavaScript011

在html中怎么用 js收藏网页到收藏夹,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:window.external.addFavorite(location.href, '我的网站')。

3、浏览器运行index.html页面,此时会弹出添加到收藏夹的面板,点击完成即可。

这个跟购物车一个概念,一般在用户没有登录的时候,将收藏的产品序号之类的放在cookie中,用户登录之后,如果cookie中有收藏的东西,就将它们转入用户对应的收藏表中,展示收藏的页面区分一下用户是否登录,然后来判断从哪里显示数据就可以了。

添加到收藏夹这一功能,不能覆盖所有浏览器,所以这里要采用渐进式增强的思想。

下面的写法兼容了IE、Firefox、Opera。而在chrome下提醒用户按快捷键收藏。

<a rel="sidebar" title="联想导航" href="http://www.idea123.cn">添加收藏</a>

<script>

$("a[rel='sidebar']").click(function(e) {

var href = this.href || document.location,

title = this.title || document.title

try{

if(window.sidebar){

sidebar.addPanel(title, href, "")

}else{

external.addFavorite(href, title)

}

}catch(e){

alert("加入收藏失败,请按Ctrl+D进行添加")

}

return false

})

</script>