給網站添加「添加到收藏夾」理論上應該是很簡單的事情,可是受到各類瀏覽器和操做系統的不一致的問題,使得這個問題異常的繁瑣啊。javascript
下面是梳理的一些資料,僅供參考。html
首先是使用快捷鍵進行添加,如咱們熟知的「Ctrl+D」,可是並非說有的電腦都支持這麼操做。鍵盤快捷鍵:Ctrl+D 僅適用於 Windows 和Linux);⌘-D 才適用於蘋果機(蘋果機上沒有Ctrl鍵)。java
方案一:使用Javascript模擬鍵盤操做web
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
BookmarkApp =
function
() {
var
isIEmac =
false
;
var
isMSIE = (-[1,]) ?
false
:
true
;
var
cjTitle =
"Welcome to CodeCTO.com"
;
var
cjHref = location.href;
function
hotKeys() {
var
ua = navigator.userAgent.toLowerCase();
var
str =
''
;
var
isWebkit = (ua.indexOf(
'webkit'
) != - 1);
var
isMac = (ua.indexOf(
'mac'
) != - 1);
if
(ua.indexOf(
'konqueror'
) != - 1) {
str =
'CTRL + B'
;
// Konqueror
}
else
if
(window.home || isWebkit || isIEmac || isMac) {
str = (isMac ?
'Command/Cmd'
:
'CTRL'
) +
' + D'
;
// Netscape, Safari, iCab, IE5/Mac
}
return
((str) ?
'Press '
+ str +
' to bookmark this page.'
: str);
}
function
isIE8() {
var
rv = -1;
if
(navigator.appName ==
'Microsoft Internet Explorer'
) {
var
ua = navigator.userAgent;
var
re =
new
RegExp(
"MSIE ([0-9]{1,}[\.0-9]{0,})"
);
if
(re.exec(ua) !=
null
) {
rv = parseFloat(RegExp.$1);
}
}
if
(rv > - 1) {
if
(rv >= 8.0) {
return
true
;
}
}
return
false
;
}
function
addBookmark(a) {
try
{
if
(
typeof
a ==
"object"
&& a.tagName.toLowerCase() ==
"a"
) {
a.style.cursor =
'pointer'
;
if
((
typeof
window.sidebar ==
"object"
) && (
typeof
window.sidebar.addPanel ==
"function"
)) {
window.sidebar.addPanel(cjTitle, cjHref,
""
);
// Gecko
return
false
;
}
else
if
(isMSIE &&
typeof
window.external ==
"object"
) {
if
(isIE8()) {
window.external.AddToFavoritesBar(cjHref, cjTitle);
// IE 8
}
else
{
window.external.AddFavorite(cjHref, cjTitle);
// IE <=7
}
return
false
;
}
else
if
(window.opera) {
a.href = cjHref;
a.title = cjTitle;
a.rel =
'sidebar'
;
// Opera 7+
return
true
;
}
else
{
alert(hotKeys());
}
}
else
{
throw
"Error occured.\r\nNote, only A tagname is allowed!"
;
}
}
catch
(err) {
alert(err);
}
}
return
{
addBookmark : addBookmark
}
}();
|
若是嫌上面的方案比較麻煩,能夠採用jQuery來快速搞定。具體實現以下: 瀏覽器
1
2
3
4
|
var
evt = jQuery.Event(
"keypress"
);
evt.keyCode = 100;
// d
evt.ctrlKey =
true
;
$(document).trigger(evt);
|
方案二:採用Javascript來添加書籤app
如下是一段能自動把當前頁面添加到瀏覽器書籤的JavaScript 腳本,支持 FireFox,Opera 和 IE,Webkit 核心的 Safari 和Chrome 暫時沒有實現相似功能的方法。ide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function
bookmark(url, title){
if
(window.sidebar)
// firefox
window.sidebar.addPanel(title, url,
""
);
else
if
(window.opera && window.print){
// opera
var
elem = document.createElement(
'a'
);
elem.setAttribute(
'href'
,url);
elem.setAttribute(
'title'
,title);
elem.setAttribute(
'rel'
,
'sidebar'
);
elem.click();
}
else
if
(document.all)
// ie
window.external.AddFavorite(url, title);
else
alert(
'Your browser does not support this function.'
);
}
|
一樣的,也提供另外的jQuery方案:post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$(document).ready(
function
(){
$(
"a.jQueryBookmark"
).click(
function
(e){
e.preventDefault();
// this will prevent the anchor tag from going the user off to the link
var
bookmarkUrl =
this
.href;
var
bookmarkTitle =
this
.title;
if
(window.sidebar) {
// For Mozilla Firefox Bookmark
window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,
""
);
}
else
if
( window.external || document.all) {
// For IE Favorite
window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
}
else
if
(window.opera) {
// For Opera Browsers
$(
"a.jQueryBookmark"
).attr(
"href"
,bookmarkUrl);
$(
"a.jQueryBookmark"
).attr(
"title"
,bookmarkTitle);
$(
"a.jQueryBookmark"
).attr(
"rel"
,
"sidebar"
);
}
else
{
// for other browsers which does not support
alert(
'Your browser does not support this bookmark action'
);
return
false
;
}
});
});
|
相應的HTML代碼以下:網站
1
|
|