开发者

Google plus one button is not visible while loading it on partial postback asp.net

开发者 https://www.devze.com 2023-04-10 09:26 出处:网络
While implementing google plus one button through addthis on one our localized pages where the following data was retrieved from a backend (assume database), facing a wierd problem, the plus button is

While implementing google plus one button through addthis on one our localized pages where the following data was retrieved from a backend (assume database), facing a wierd problem, the plus button is not getting loaded. we ajax based partial postback

The following html is added to the page only during a partial postback

<div class="addthis_toolbox addthis_default_style" style="float:right;">
   <a class="addthis_button_google_plusone" g:plusone:size="small"></a>
</div>

At the same time , the script is included through

ScriptManager.RegisterStartupScript(Page, typeof(string),
                                    "RegisterHTMLScript" + scriptID,
                                     scriptstring,
                                     false); 

Value of scriptstring is

<script type="text/javascript">
     var addthis_config = {"data_track_clickback":true};
     if (window.addthis) {
        addthis.toolbox('.addthis_toolbox'); 
        addthis.init(); 
     }
     else {
       $.ge开发者_StackOverflow社区tScript('http://s7.addthis.com/js/250/addthis_widget.js#username=XXXXX&domready=1'); 
       $.getScript('https://apis.google.com/js/plusone.js'); 
     }
 </script>

I am not able to see any client errors, but at the same time +1 button is not visible. What could be wrong?

Instead of RegisterStartupScript, I tried 1. adding ScriptReference programatically to the current script manager. 2. RegisterClientScriptInclude method on script manager.

But almost same result.


Here it goes,

During a partial postback, ASP.NET AJAX needs to know the list of dependent scripts that are to be downloaded so that it can announce "DOCUMENT IS READY" once all of them are downloaded. In ASP.NET 3.5 and below adding "notifyscriptloaded" is the only means for ASP.NET to know a dependent script resource has to be loaded - during a partial postback.

When you register a script with notifyscriptloaded, ASP.NET waits till the script is downloaded and makes sure to call document ready only after downloading this. This way it ensures document ready of the script is being fired as expected. NOTE: ASP.NET 4.0 does NOT need this (I am not sure how it is done in it however).

So the problem now is very clear. When using an external 3rd party javascript file as a reference, ASP.NET limits us to refer it only from static page header (or master page). There is no way to download the script during partial postback - Unless you have the control over thhird party script and add "notifyscriptloaded" in it.

Solution, I had to make this external third party script a static reference in page header. This did not hurt me because, I anyway had several instances of +1 button across multiple pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号