4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

パラメータ付きURLって、SNSでシェアできないの?(Twitter・Facebook)

Last updated at Posted at 2020-03-12

#できます
ただし一手間が必要。
それはエンコード

#実装方法

// URLとパラメータを分ける
var href = location.href;
var param = location.search;
var url = href.replace(param, '');

// パラメータをエンコード
param = encodeURIComponent(param);

// 各SNSのシェアURLと、シェアしたいURL、パラメータ、ハッシュタグを結合
// Twitter
var twLink = 'http://twitter.com/share?url=' + url + param + '&hashtags=ハッシュタグ'; 
// Facebook
var fbLink = 'https://www.facebook.com/sharer/sharer.php?u=' + url + param;

// シェアボタンのリンクを置き換える
// Twitter
$('#js-tw-link').attr('href', twLink);
// Facebook
$('#js-fb-link').attr('href', fbLink);

encodeURIComponent()にパラメータを入れる一手間が必要。
最初、http://〜からURL全体をエンコードしてたのでうまくいきませんでした。

4
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?