프로그래밍/JavaScipt

javascript sns공유 스크립트

p-a-r-k 2016. 7. 19. 10:27
반응형
var SOCIAL_SHARE = {
	kakaostory: {
		url: "https://story.kakao.com/share",
		width: 640,
		height: 600,
		makeShareUrl: function(url) {
			return this.url + "?url="+encodeURIComponent(url);
		}
	},
	facebook: {
		url: "https://www.facebook.com/sharer/sharer.php",
		width: 640,
		height: 400,
		makeShareUrl: function(url) {
			return this.url + "?display=popup&u=" + encodeURIComponent(url) +"&t=" + encodeURIComponent("내용");
		}
	},
	twitter: {
		url: "http://twitter.com",
		width: 640,
		height: 400,
		makeShareUrl: function(url) {
			return this.url + "?status="+encodeURIComponent("content_example ")+" "+encodeURIComponent(url);
		}
	},
	naver: {
		url: "http://share.naver.com/web/shareView.nhn",
		width: 640,
		height: 600,
		makeShareUrl: function(url) {
			return this.url + "?url="+encodeURIComponent(url) +"&title="+ encodeURIComponent("내용");
		}
	}
};

var onClick = function(e) {
	e.preventDefault();
	var $this = $(this),
		 service = SOCIAL_SHARE[$this.data("service")],
		 url = "공유할 링크";

	if (service) {
		e.preventDefault();
		window.open(service.makeShareUrl(url), "", "width=" + service.width + ", height=" + service.height);
	}
};
$(".sns_box").off("click", "a").on("click", "a", onClick);
반응형