お気に入り(ブックマーク)への追加ボタンを設置する

お気に入り(ブックマーク)への登録は、ブラウザごとに実装が異なりやっかいです。

navigator.userAgentによって、ブラウザの種類を振り分けます。

以下のスクリプトでは、IE,Firefox,Operaの3ブラウザに対応しています。

<script type="text/javascript">
<!--
//お気に入りに追加(各種ブラウザ対応)
/*
  IE
*/
if(navigator.userAgent.indexOf("MSIE") > -1){ //Internet Explorer
document.write('<!-'+'-[if IE]>');
document.write('<input type="button" value="お気に入りに追加"');
document.write(' onclick="window.external.AddFavorite(\'http://abc.com/\',\'サイト名\')">');
document.write('<![endif]-'+'->');
}
/*
  FireFox
*/
else if(navigator.userAgent.indexOf("Firefox") > -1){ //Firefox
document.write('<input type="button" value="ブックマークに追加"');
document.write(' onclick="window.sidebar.addPanel(\'サイト名\',\'http://abc.com/\',\'\');">');
}
/*
  Opera
*/
else if(navigator.userAgent.indexOf("Opera") > -1){ //Opera
document.write('<a href="http://abc.com/" rel="sidebar" title="サイト名">ブックマークに追加</a>');
}
/*
  その他
*/
else { //該当なし
void(0); //何もしない
}
//-->
</script>

 
 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です