代码之家  ›  专栏  ›  技术社区  ›  Eliot Sykes

使用Prototype从另一个域加载JavaScript文件

  •  1
  • Eliot Sykes  · 技术社区  · 16 年前

    使用Prototype,任何人都知道如何使用Ajax.请求从另一个域?或者如果可能的话?

    我相信jquery是有可能的,digg这样做是为了加载Facebook API:

    jQuery.ajax({type:"GET",
    url:"http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php",
    cache:true, dataType:"script"});
    

    资料来源: http://cotnet.diggstatic.com/js/loader/370/digg_facebook

    如果不看代码,我猜jquery在url违反同源策略并且数据类型为script时可以使用代理。

    2 回复  |  直到 14 年前
        1
  •  0
  •   Tomas Vana    16 年前

    检查 this 出去。。似乎有一个特定的插件可以实现Prototype库中的功能,笔者提到jQuery已经支持它很长时间了,但它似乎并不支持默认的It Prototype。

        2
  •  0
  •   Eliot Sykes    16 年前

    var FacebookApiLoader = Class.create({
      initialize: function() {
        this.observer = null
        this.observedElement = null
      },
      apiDependentsVisible: function() {
        if (null == this.observedElement) {
          // $('facebook-login') is a div in my site that
          // is display:none initially.  Once it is made
          // visible then the facebook api is needed.
          // Replace 'facebook-login' with id relevant for your site
          this.observedElement = $('facebook-login')
        }
        return this.observedElement.visible()
      },
      apiLoadCompleted: function() {
        try {
          return !Object.isUndefined(FB) && !Object.isUndefined(FB_RequireFeatures)
        } catch (e) {
        }
        return false
      },
      initAndRequireFeatures: function() {
        FB_RequireFeatures(["XFBML"],
          function() {
            FB.init('secret-put-your-app-value-here','/xd_receiver.html', {})
          }
        );
      },
      initFacebookConnect: function() {
        new PeriodicalExecuter(function(pe) {
          if (this.apiLoadCompleted()) {
            this.initAndRequireFeatures()
            pe.stop()
          }
        }.bind(this), 0.2);
      },
      loadApi: function() {
        // Use body for facebook script as recommended in Facebook
        // docs not to insert in head as some browsers have 
        // trouble with it
        body = $$('body')[0]
        // TODO use https protocol if page is secure
        script = new Element('script', { 'type': 'text/javascript',
          'src': 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php' })
        body.appendChild(script)
        this.initFacebookConnect()
      },
      loadApiIfRequired: function() {
        if (this.apiDependentsVisible()) {
          this.observer.stop()
          this.loadApi()
        }
      },
      observe: function() {
        if (null == this.observer) {
          this.observer = new PeriodicalExecuter(this.loadApiIfRequired.bind(this), 0.2)
        }
      }
    });
    // The FacebookApiLoader attributes are lazily loaded
    // so creating a new facebookApiLoader
    // is as low resource usage as possible
    var facebookApiLoader = new FacebookApiLoader();
    

    然后在任何可能需要Facebook api的页面上,调用

    facebookApiLoader.observe();