[英]Addthis is Undefined when I use addthis, When I load page by Ajax
I'm try to show 'addthis sharing' buttons when make ajax call. 我尝试在进行ajax调用时显示“ addthis共享”按钮。 at the first call by ajax, buttons does not show, but when I reload whole page everything is OK, buttons is right place. 在ajax的第一次调用中,按钮不显示,但是当我重新加载整个页面时,一切正常,按钮在正确的位置。
I searched a lots of fixes but no one works for me. 我搜索了很多修复程序,但没有人适合我。 one of them is addthis.toolbox(); 其中之一是addthis.toolbox(); or window.addthis but when I use word addthis insde JavaScript tag, browser debugger writes error 'addthis is undefined'. 或window.addthis,但是当我使用单词addthis insde JavaScript标记时,浏览器调试器将写入错误“ addthis is undefined”。
please give me smart advice what's happen and how can I fix it ? 请给我明智的建议,怎么办?如何解决?
Code (it's a partial view which load from ajax Call): 代码(这是从ajax调用加载的局部视图):
<script type="text/javascript" src="http://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-xxxxx" async="async"></script>
<div class="addthis_sharing_toolbox"></div>
<script>
addthis.toolbox(); // addthis - is undefined
</script>
I have fixed this problem. 我已经解决了这个问题。
In my project I have 3 View level 在我的项目中,我有3个视图级别
I had addthis Js reference and button's Div inside the _partialView. 我在_partialView中添加了这个Js参考和按钮的Div。
But when I move Js reference to View and change Url(add - &async=1) it works fine and now 'addthis' - is defined (till here is undefined). 但是,当我将Js引用移至View并更改Url(add-&async = 1)时,它可以正常工作,并且现在定义了“ addthis”(直到此处未定义)。
Hare is full example: 野兔是一个完整的例子:
View: 视图:
<script type="text/javascript" src="http://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-'yourPubId'&async=1"></script>
//Some Code
_partialView: _partialView:
//Some Code
<div class="addthis_sharing_toolbox"></div>
<script>
$(function() {
addthis.init();
addthis.layers.refresh();
});
</script>
Good luck, everyone can use this perfect plugin 'addthis' when you load page by Ajax. 祝您好运,当您通过Ajax加载页面时,每个人都可以使用这个完美的插件“ addthis”。
The async version of the addthis_widget.js script you're using was intended to be used for the newer dashboard tools, as the call to addthis.toolbox() is undefined because AddThis hasn't fully loaded yet. 您正在使用的addthis_widget.js脚本的异步版本旨在用于较新的仪表板工具,因为尚未完全加载AddThis,所以未定义对addthis.toolbox()的调用。 If you remove async="async" from the script, it should work. 如果从脚本中删除async =“ async”,它应该可以正常工作。
Alternatively, you could add the async attribute this way: 或者,您可以通过以下方式添加async属性:
<script src="http://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-xxxxx&async=1" type="text/javascript">
Then before you call addthis.toolbox(), make sure you call addthis.init(). 然后,在调用addthis.toolbox()之前,请确保调用addthis.init()。
https://www.addthis.com/blog/2013/05/07/a-brief-history-of-using-addthis-dynamically/ https://www.addthis.com/blog/2013/05/07/a-brief-history-of-using-addthis-dynamically/
-Matt -马特
AddThis Support 添加此支持
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.