Skip to content

Instantly share code, notes, and snippets.

@cyokodog
Last active October 13, 2015 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyokodog/4108668 to your computer and use it in GitHub Desktop.
Save cyokodog/4108668 to your computer and use it in GitHub Desktop.
jQuery Hatebu

#jQuery Hatebu

はてブボタンを生成する jQuery プラグイン。

記事ページでの使用例。

//ボタンタグの生成、挿入
$.hatebu().appendTo('#social-buttons');

//レンダリング
$.hatebu.rendar();

一覧ページでの使用例。

//ボタンタグの生成、挿入
var social = $('.post .social-buttons');
$('.post h2 a').each(function(idx){
	var target = $(this);
	$.hatebu({
		url : target.prop('href'), // url
		title : target.text() // title
	}).appendTo(hatebu.eq(idx));
});

//レンダリング
$.hatebu.rendar();
(function(){
$.hatebu = function(option){
var c = $.extend({},$.hatebu.defaults,option);
if(!c.title && c.url == location.href) c.title = $('title').text();
var img = $('<img style="border:none;width:20px;height:20px"/>').
prop({
src : 'http://b.st-hatena.com/images/entry-button/button-only.gif',
alt : c.alt
})
var link = $('<a/>').
addClass('hatena-bookmark-button').
prop({
href : "http://b.hatena.ne.jp/entry/" + encodeURIComponent(c.url),
title : c.alt
}).data({
'hatena-bookmark-title' : c.title,
'hatena-bookmark-layout' : c.layout
});
return link.append(img);
}
$.hatebu.rendar = function(){
$('<script/>').prop({
charset : 'utf-8',
async : 'async',
src : 'http://b.st-hatena.com/js/bookmark_button.js'
}).appendTo('body');
}
$.hatebu.defaults = {
url : location.href,
title : '',
layout : 'simple-balloon',
alt : 'このエントリーをはてなブックマークに追加'
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment