Here is a simple code to get a Twitter widget. Firstly, all we need to get is the Twitter widget id.

So follow these steps to get widget id from twitter.
Step 1: Create an account on Twitter if you don't have one and login on Twitter.
Step 2: Go to Settings
Step 3: Click on widget in left bottom.
Step 4: Click on Create new button as follows:

Step 5: Click on Create widget button.
Step 6: Finally your widget has been created and you will get the widget id as follows:
  1. <input type="hidden" id="HiddenWidgetKey" value="your budget-id" /> <%--like xxx176400881455xxx--%>
  2. <span class="normalGray">Twitter handle</span>
  3. <asp:TextBox ID="TextBoxTwitterhandle" runat="server" Style="width: 350px;" placeholder="Enter TwitterTwitter handle"
  4. CssClass="textBox" TabIndex="120" ClientIDMode="Static">
  5. </asp:TextBox>
  6. <input type="button" name="save" value="Save" onclick="GetResult();" />
  7. <div id="tweetBtn">
  8. </div>
  9. <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
  10. <script src="<%= ResolveUrl("~/Scripts/Twitter/customize-twitter.js")%>" type="text/javascript"></script>
  11. <style>
  12. .HeadingPinkSummary
  13. {
  14. color: #D9509C;
  15. font-size: 19px;
  16. }
  17. </style>
Customize-twitter.js:
  1. var CustomizeTwitterWidget = function (data) {
  2. if (!(window.console && console.log)) {
  3. console = {
  4. log: function () { },
  5. debug: function () { },
  6. info: function () { },
  7. warn: function () { },
  8. error: function () { }
  9. };
  10. }
  11. var notNumeric = function (n) {
  12. return isNaN(parseFloat(n)) && isFinite(n);
  13. };
  14. var createCssElement = function (doc, url) {
  15. var link = doc.createElement("link");
  16. link.href = url;
  17. link.rel = "stylesheet";
  18. link.type = "text/css";
  19. return link;
  20. };
  21. var embedCss = function (doc, url) {
  22. var link = createCssElement(doc, url);
  23. var head = doc.getElementsByTagName("head")[0];
  24. head.appendChild(link);
  25. };
  26. var contains = function (haystack, needle) {
  27. return haystack.indexOf(needle) >= 0;
  28. };
  29. var isTwitterFrame = function (frame) {
  30. return frame.frameElement.id.indexOf('twitter-widget') >= 0;
  31. }
  32. /**
  33. * The main event loop - calls itself if we haven't found all of the frames
  34. * yet.
  35. */
  36. var evaluate = function (framesWithStyles, widgetCount, timeoutLength) {
  37. for (var i = 0; i < frames.length; i++) {
  38. try {
  39. if (isTwitterFrame(frames[i]) &&
  40. !contains(framesWithStyles, frames[i].name)
  41. ) {
  42. embedCss(frames[i].document, data.url);
  43. framesWithStyles.push(i);
  44. }
  45. } catch (e) {
  46. console.log("caught an error");
  47. console.log(e);
  48. }
  49. }
  50. if (framesWithStyles.length < widgetCount) {
  51. setTimeout(function () {
  52. evaluate(framesWithStyles);
  53. }, timeoutLength);
  54. }
  55. }
  56. if (data.url === undefined) {
  57. console.log("need to specify a link to your CSS file. quitting");
  58. return;
  59. }
  60. var widgetCount;
  61. if (data.widget_count === undefined || notNumeric(data.widget_count)) {
  62. widgetCount = 1;
  63. } else {
  64. widgetCount = data.widget_count;
  65. }
  66. var timeoutLength;
  67. if (data.timeout_length === undefined || notNumeric(data.timeout_length)) {
  68. timeoutLength = 300;
  69. } else {
  70. timeoutLength = data.timeout_length;
  71. }
  72. setTimeout(function () {
  73. evaluate([], widgetCount, timeoutLength);
  74. }, timeoutLength);
  75. }
Now write the javascript code as follows:
  1. <script type="text/javascript">
  2. function GetResult() {
  3. GetTwitter();
  4. }
  5. window.twttr = (function (d, s, id) {
  6. var t, js, fjs = d.getElementsByTagName(s)[0];
  7. if (d.getElementById(id)) return; js = d.createElement(s); js.id = id;
  8. js.src = "https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
  9. return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f) } });
  10. } (document, "script", "twitter-wjs"));
  11. var screenName = "";
  12. function GetTwitter() {
  13. screenName = jQuery("#TextBoxTwitterhandle").val();
  14. ReloadTwitterWidget();
  15. jQuery("#twitter-timeline").attr("data-screen-name", screenName)
  16. !function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + "://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); } } (document, "script", "twitter-wjs");
  17. var options = {
  18. "url": '<%= ResolveUrl("~/Scripts/Twitter/twitter.css")%>'
  19. };
  20. CustomizeTwitterWidget(options);
  21. }
  22. function ReloadTwitterWidget() {
  23. var widgetkey = jQuery("#HiddenWidgetKey").val();
  24. screenName = jQuery("#TextBoxTwitterhandle").val();
  25. jQuery('#tweetBtn iframe').remove();
  26. var html = " <a class='twitter-timeline' data-widget-id='" + widgetkey + "' data-screen-name='" + screenName + "' data-tweet-limit='10' id='twitter-timeline' data-text=\"REPLACEME\" ></a>"
  27. jQuery("#tweetBtn").append(html);
  28. twttr.ready(function (twttr) {
  29. twttr.widgets.load();
  30. });
  31. }
  32. </script>
Output: Now press f5,
Enter your twitter handle and click on save.
Congrats! Your widget displayed as follows:

Read more articles on jQuery: