如何在asp按钮上单击调用以下脚本

时间:2013-06-24 08:30:57

标签: javascript asp.net facebook-javascript-sdk

而不是div中以下脚本的默认用法我想在按钮上点击脚本

 <div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1">
        </div>
        <script language="javascript" type="text/javascript">
        window.fbAsyncInit = function () {
            FB.init({
                appId: 'xxxxxxxxxxx',
                status: true,
                cookie: true,
                xfbml: true
            });
        };

        (function (d) {
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) {
                return;
            }
            js = d.createElement('script');
            js.id = id;
            js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        } (document));
    </script>

有人可以帮助我如何在asp按钮点击事件上调用或执行此脚本。

1 个答案:

答案 0 :(得分:0)

修改

您的代码不完整,我更新了代码,这适用于我的域名,因此,它也必须适用于您的代码 把这个js代码放在a.js文件中,按下按钮时会调用它:

window.fbAsyncInit = function() {
  FB.init({
    appId      : 'your_app_id', // App ID
    channelUrl : 'your_app_domain', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

  // Here we subscribe to the auth.authResponseChange JavaScript event
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
      // logged into the app
    alert('logged');
    } else if (response.status === 'not_authorized') {
      // In this case, the person is logged into Facebook, but not into the app
    alert('non logged');
    FB.login();
    } else {
      // not logged into Facebook
    alert('non logged everywhere');
    FB.login();
    }
  });
  };
  // Load the SDK asynchronously
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));

然后将此代码放到要调用它的页面上,记住fb-root div和fb所需的所有其他内容:

<div id="fb-root"></div>
<div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1">
        </div>
<button onClick="loadit()">ZzZzz</button>

<script type="text/javascript">
function loadit(){
var head= document.getElementsByTagName('head')[0];
   var script= document.createElement('script');
   script.type= 'text/javascript';
   script.src= 'a.js';
   head.appendChild(script);

}

结束编辑

您可以通过以下方式从javascript加载脚本(例如我们调用文件a.js): 您将把js代码放在文件a.js中 检查它是否有效或需要进行一些改进

<button onClick="loadit()">ZzZzz</button>

<script type="text/javascript">
function loadit(){
var head= document.getElementsByTagName('head')[0];
   var script= document.createElement('script');
   script.type= 'text/javascript';
   script.src= 'a.js';
   head.appendChild(script);
}
</script>

(您不需要隐藏应用ID,它可以通过js看到,除了您之外,没有其他人可以在指定的域外使用它。)