javascript - Uncaught TypeError: t.querySelectorAll is not a function when initialiazing Embedded Tweets - Stack Overflow

I'm using the Embedded Tweets API from Twitter.Everything works great if I have the html markdown

I'm using the Embedded Tweets API from Twitter. Everything works great if I have the html markdown from page load. It also works great if I use the option to add content later, and load the tweets by ID:

twttr.widgets.load(
  document.getElementById("container")
);

This is in the documentation here:

But If I try to load the tweets by class name with this:

twttr.widgets.load(
  document.getElementsByClassName("containers")
);

I'm getting this error inside Twitter's widget.js:

Uncaught TypeError: t.querySelectorAll is not a function

If I log the group of elements to the console, I have a proper group:

Am I doing something wrong or is this a bug on Twitter's widget?

Thanks!

EDIT:

As of July 28th 2015, the getElementsByClassName example has been removed from Twitter's documentation thanks to another post I made on the Twitter Dev Forums

I'm using the Embedded Tweets API from Twitter. Everything works great if I have the html markdown from page load. It also works great if I use the option to add content later, and load the tweets by ID:

twttr.widgets.load(
  document.getElementById("container")
);

This is in the documentation here: https://dev.twitter./web/javascript/initialization#init

But If I try to load the tweets by class name with this:

twttr.widgets.load(
  document.getElementsByClassName("containers")
);

I'm getting this error inside Twitter's widget.js:

Uncaught TypeError: t.querySelectorAll is not a function

If I log the group of elements to the console, I have a proper group:

Am I doing something wrong or is this a bug on Twitter's widget?

Thanks!

EDIT:

As of July 28th 2015, the getElementsByClassName example has been removed from Twitter's documentation thanks to another post I made on the Twitter Dev Forums

Share Improve this question edited Jul 28, 2015 at 15:58 Jan asked Jul 16, 2015 at 15:42 JanJan 2,5204 gold badges33 silver badges60 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3 +50

The problem may e from the fact that document.getElementsByClassName returns a NodeList type which is not real javascript Array object. On the other hand, widgets.load expects either a single element or an Array of elements. This leads nodeList to be treated as a single element and the library tries to call querySelectorAll on it which is not available for NodeList types.

A solution would be to convert that nodeList to a real array when passing it to widgets.load.

One way:

twttr.widgets.load(
  Array.prototype.slice.call(document.getElementsByClassName("containers"))
);

This works, because a NodeList has a length property and is indexable.

According to twitter's documentation, load() accepts either no param or a single element.

Called without argument, widgets-js will search the entire document.body DOM tree for uninitialized widgets. For better performance, pass an HTMLElement object to restrict the search only to children of the element.

Since getElementsByClassName returns an HTMLCollection, you can't pass that directly. Instead you could pass the first element in the collection like this

twttr.widgets.load(
  document.getElementsByClassName("containers")[0];
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744869762a4598189.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信