[JavaScript]跨浏览器的加入书签
1
function addBookmark() {
2
var links = document.getElementsByTagName('a');
3
if(!links) return;
4
for(var i=0; i<links.length; i++) {
5
if(links[i].getAttribute('rel') == 'bookmark') {
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
var p_title = document.title;
8
var p_url = window.location.href;
9
if (document.all && !window.opera){
10
links[i].setAttribute('href', "javascript:window.external.AddFavorite('" + p_url + "', '" + p_title + "')");
11
}
12
else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')){
13
links[i].setAttribute('href', "javascript:window.sidebar.addPanel ('" + p_title + "', '" + p_url + "','')");
14
}
15
else {
16
links[i].href = "" + p_url + "";
17
}
18
}
19
}
20
}
利用rel属性,将其值设置为bookmark即可为a元素添加此功能。![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
参考来源:
http://home.arcor.de/xbo/sidebar-cross-browser/