简体   繁体   English

twitter的个人资料小部件包含在单独的javascript文件中

[英]twitter's profile widget include in a separate javascript file

I'm just trying to put the custom twitter's profile widget on my page, but I like to put the code in a separate javascript's file. 我只是想将自定义twitter的配置文件小部件放在我的页面上,但我想将代码放在单独的javascript文件中。

so, I don't know how to do that. 所以,我不知道该怎么做。

I mean, I put this on head tag 我的意思是,我把它放在头标签上

<script type="text/javascript" src="http://widgets.twimg.com/j/2/widget.js"></script>

the create a div for the widget, an put the rest of the code in another javascript 为小部件创建一个div,将其余代码放入另一个javascript

new TWTR.Widget(json_twitter_options).render().setUser('username').start();

But, how to "put" the result in that widget... I'm totally lost, thanks in advance. 但是,如何将结果“放入”该小部件中……我已经完全迷失了,谢谢。

The solution for me was simply to add an "id" property to the options object of an element that you want the widget to render into. 对我来说,解决方案只是向您希望小部件呈现到其中的元素的options对象添加一个“ id”属性。 This way you can even load the script with require.js or after the page loads and still render the widget into the space you want. 这样,您甚至可以使用require.js或在页面加载后加载脚本,并且仍将小部件呈现到所需的空间中。

I didn't find any documentation for this property by the way. 顺便说一下,我没有找到此属性的任何文档。

ex: 例如:

new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 4,
  interval: 6000,
  width: 135,
  height: 700,
  theme: {
    shell: {
      background: '#e5e5e5',
      color: '#363F49'
    },
    tweets: {
      background: '#e5e5e5',
      color: '#000000',
      links: '#06C'
    }
  },
  features: {
    scrollbar: false,
    loop: false,
    live: false,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
  },
  id: 'someDivId'
}).render().setUser('comster').start();

Just put the entire code into a new javascript file (name it anything you like, like twitter.js), and link it in where you want it to display, like under 只需将整个代码放入一个新的javascript文件(将其命名为twitter.js之类的任何名称),然后将其链接到想要显示的位置即可,例如

<div id="twittercode">[js file link goes here]</div>

This whole chunk should go into a new javascript file. 这整个块应该放入一个新的javascript文件中。

new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 4,
  interval: 6000,
  width: 135,
  height: 700,
  theme: {
    shell: {
      background: '#e5e5e5',
      color: '#363F49'
    },
    tweets: {
      background: '#e5e5e5',
      color: '#000000',
      links: '#06C'
    }
  },
  features: {
    scrollbar: false,
    loop: false,
    live: false,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
  }
}).render().setUser('USERNAME').start();

The js file doesn't have to be in the head just above the widget js文件不必位于小部件上方的头部

this code works I have tested it: 此代码有效,我已经对其进行了测试:

<script src="http://widgets.twimg.com/j/2/widget.js"></script> 
<script> 
new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 4,
  interval: 6000,
  width: 135,
  height: 700,
  theme: {
    shell: {
      background: '#e5e5e5',
      color: '#363F49'
    },
    tweets: {
      background: '#e5e5e5',
      color: '#000000',
      links: '#06C'
    }
  },
  features: {
    scrollbar: false,
    loop: false,
    live: false,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
  }
}).render().setUser('USERNAME').start();
</script>

Ok, It seem my question was not understand. 好的,看来我的问题不明白。

I couldn't do what I want. 我做不到我想要的。 But the way to simplify my problem, was creating a json object for setting up the widget. 但是简化我的问题的方法是创建一个用于设置小部件的json对象。

I'm still want to find a way to not use script tag inside body tag 我仍然想找到一种在正文标签内不使用脚本标签的方法

thanks a lot to viewvers! 非常感谢观看者!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM