Skip to content

Instantly share code, notes, and snippets.

@gaogao-9
Last active March 27, 2016 16:20
Show Gist options
  • Save gaogao-9/6497fa78becacc347b4b to your computer and use it in GitHub Desktop.
Save gaogao-9/6497fa78becacc347b4b to your computer and use it in GitHub Desktop.
動作未確認です
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 省略 -->
<script src="widget.js"></script>
<script src="index.js"></script>
</head>
</html>
window.twttr.ready(function(){
var elm = document.getElementById('親となる要素');
twttr.widgets.createTimeline('タイムラインID', elm, {
/* オプションは https://dev.twitter.com/web/javascript/creating-widgets#create-timeline */
}).then(function(iframe){
// ここでいろいろする
iframe.style.width = "100%";
});
});
// 例のスクリプトがゴミすぎたので改良した話
window.twttr = (function(d, s, id){
var readyEvents = [];
if(document.readyState !== "loading"){
window.twttr = createTwttr(d, s, id);
return;
}
document.addEventListener("DOMContentLoaded", registerTwttr, false);
// ダミーを作っておく
return {
ready: function(callback){
readyEvents.push(callback);
}
};
function registerTwttr(eve){
window.twttr = createTwttr(d, s, id);
document.removeEventListener("DOMContentLoaded", registerTwttr, false);
}
function createTwttr(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = readyEvents;
t.ready = function(f){
t._e.push(f);
};
return t;
}
})(document, "script", "twitter-wjs");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment