Search code examples
javajavascripthtmlfacebookgwt

GWT <script> tag not loading properly


I have defined in my head tag a function <script> functionX(a,b,c) ....</script>

which calls some third party js. The actual function is this:

    <script>
    function fbc(d, s, id) 
    {     var js, fjs = d.getElementsByTagName(s)[0];
          if    (d.getElementById(id)) {return;}    
          js = d.createElement(s); js.id = id;      
          js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";  
          fjs.parentNode.insertBefore(js, fjs);   
    }
    </script>

and later on I want to call that function. I'm calling it using and HTML widget in the following way:

    HTML html = new HTML();
    setHTML(
            "<script>"+
            "   fbcomment(document, 'script', 'facebook-jssdk');   "+
            "</script> "+  
            "<fb:comments width=\"600\" num_posts=\"2\" href=\"http://www.woojah.com\"></fb:comments>");

However, it is not executing properly. Any advice would be very useful.


Solution

  • It is more idiomatic to call external functions using JSNI:

    native void facebookComment(Document d, String s, String id) /*-{
      fbc(d, s, id);
    }-*/;
    
    void myFunction() {
      facebookComment(Document.get(), "script", "facebook-jssdk");
    }