简体   繁体   English

通过JavaScript插入时,Facebook“赞”按钮为什么不遵守其参数?

[英]Why doesn't the Facebook “Like” button honor its parameters when inserted via JavaScript?

I just discovered that the iframe version of the Facebook Like button doesn't honor its query parameters when the iframe is created with JavaScript, rather than included directly in the document's HTML. 我刚刚发现,使用JavaScript创建iframe而不是直接将其包含在文档的HTML中时,Facebook Like按钮iframe版本不支持其查询参数。

Please have a look at this jsFiddle that I created: 请看一下我创建的这个jsFiddle:

http://jsfiddle.net/qQsCC/ http://jsfiddle.net/qQsCC/

I generated a Like button at the URL linked above and first included the HTML exactly as it was provided. 我在上面链接的URL上生成了一个Like按钮,并首先完全按所提供的那样包含HTML。 Then, I broke it down into the JavaScript code needed to create and append an identical element to the DOM. 然后,我将其分解为创建相同的元素并将其添加到DOM所需的JavaScript代码。

In the "Result" window, you'll see the HTML version of the button on top, and the JavaScript-created version below. 在“结果”窗口中,您将在顶部看到按钮的HTML版本,并在下面看到JavaScript创建的版本。 While the value of the src attribute is identical for both (as well as all other HTML attributes), the lower button doesn't appear to honor any of the parameters that I've passed, such as colorscheme or font . 尽管src属性的值(以及所有其他HTML属性)都是相同的,但下部的按钮似乎并不适合我传递的任何参数,例如colorschemefont

Does anyone know why this is happening, or have any suggestions for how I might avoid this behavior? 有谁知道为什么会这样,或者对我如何避免这种行为有任何建议?

The use case here is that I'm creating HTML ads that will include the iframe version of the "Like" button; 这里的用例是,我正在创建HTML广告,其中将包含iframe版本的“赞”按钮; a requirement is that the ad can only load 50KB of data initially, then up to 1MB after window.onload has fired. 要求是广告最初只能加载50KB数据,然后在window.onload触发后最多加载1MB。 Since the "Like" button weighs in over 50KB alone, I need to construct the iframe using JavaScript after window.onload rather than just including the <iframe> element in the ad's HTML. 由于“赞”按钮仅重50KB,因此我需要在window.onload之后使用JavaScript构建iframe,而不仅仅是在广告的HTML中添加<iframe>元素。

When you add url using HTML, html entities are automatically decoded. 使用HTML添加url时,html实体会自动解码。 This doesn't nappen in javascript. 这不会出现在javascript中。 So you need to decode the url before passing it to javasript eg: 因此,您需要在将URL传递给javasript之前对其进行解码,例如:

like.src = 'http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F&send=false&layout=standard&width=450&show_faces=false&action=like&colorscheme=dark&font=arial&height=35';

Hope this helps 希望这可以帮助

Updated JSfidle: http://jsfiddle.net/qQsCC/1/ 更新的JSfidle: http : //jsfiddle.net/qQsCC/1/

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

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