Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将模板变量用作脚本src的一部分_Javascript_Jquery_Google Apps Script_Paypal - Fatal编程技术网

Javascript 将模板变量用作脚本src的一部分

Javascript 将模板变量用作脚本src的一部分,javascript,jquery,google-apps-script,paypal,Javascript,Jquery,Google Apps Script,Paypal,我正在尝试创建一个模型付款弹出窗口,用户可以在谷歌应用程序脚本中升级他们的计划。在我的.gs文件中,我有: function manageSubscription() { var title = 'subscribe'; var html = HtmlService.createTemplateFromFile('subscribe'); html.email = Session.getActiveUser().getEmail(); var htmlOutput = html.

我正在尝试创建一个模型付款弹出窗口,用户可以在谷歌应用程序脚本中升级他们的计划。在我的
.gs
文件中,我有:

function manageSubscription() {
  var title = 'subscribe';
  var html = HtmlService.createTemplateFromFile('subscribe');
  html.email = Session.getActiveUser().getEmail();
  var htmlOutput = html.evaluate();
  htmlOutput.setTitle(title).setWidth(200).setHeight(200)
  DocumentApp.getUi().showModalDialog(htmlOutput, title);
}

在我的
.html
文件中,我试图在他们注册时使用该电子邮件地址将其传递给paypal:

  <script id="paypal_js"></script> // how do I set the src???
  <script>
  $(document).ready(function(){
    var src = 'https://www.paypal.com/sdk/js?client-id=my-client-id&email=';
    document.getElementById('paypal_js').src = src+<?= email ?>;

    paypal.Buttons({
      createSubscription: function(data, actions) {
        return actions.subscription.create({'plan_id': 'P-my-plan'});
      },
      onApprove: function(data, actions) {
        google.script.host.close();
      }
    }).render('#paypal-button-container');
  });
  </script>
//如何设置src???
$(文档).ready(函数(){
var src='1〕https://www.paypal.com/sdk/js?client-id=我的客户id和电子邮件=';
document.getElementById('paypal_js')。src=src+;
贝宝,按钮({
createSubscription:函数(数据、操作){
返回actions.subscription.create({'plan_id':'P-my-plan'});
},
onApprove:功能(数据、操作){
google.script.host.close();
}
}).render(“#贝宝按钮容器”);
});

但是,我得到了
ReferenceError:paypal没有在浏览器控制台中定义。奇怪的是,我可以做一个简单的“alert();”请注意我收到了电子邮件。

问题在于您的代码使用的是
paypal
, 您正在动态添加
script
,我假设您将从该脚本中获得
paypal
作为全局对象

您忘记的是,在执行
paypal.Buttons
的代码时,
paypal
在浏览器中不可用。因为,它仍然可能从脚本中获取和评估代码,您刚才包括了。您必须侦听脚本onload事件,然后调用所需的函数。

<script id="paypal_js"></script> // how do I set the src???
  <script>
  $(document).ready(function(){
    var src = 'https://www.paypal.com/sdk/js?client-id=my-client-id&email=';
    var scriptTag = document.getElementById('paypal_js')
    scriptTag.src = src+<?= email ?>;

    scriptTag.onload = function() {
      paypal.Buttons({
        createSubscription: function(data, actions) {
          return actions.subscription.create({'plan_id': 'P-my-plan'});
        },
        onApprove: function(data, actions) {
          google.script.host.close();
        }
      }).render('#paypal-button-container');
    }
  });
  </script>
//如何设置src???
$(文档).ready(函数(){
var src='1〕https://www.paypal.com/sdk/js?client-id=我的客户id和电子邮件=';
var scriptTag=document.getElementById('paypal_js')
scriptTag.src=src+;
scriptTag.onload=函数(){
贝宝,按钮({
createSubscription:函数(数据、操作){
返回actions.subscription.create({'plan_id':'P-my-plan'});
},
onApprove:功能(数据、操作){
google.script.host.close();
}
}).render(“#贝宝按钮容器”);
}
});

问题在于您的代码使用的是
paypal
, 您正在动态添加
script
,我假设您将从该脚本中获得
paypal
作为全局对象

您忘记的是,在执行
paypal.Buttons
的代码时,
paypal
在浏览器中不可用。因为,它仍然可能从脚本中获取和评估代码,您刚才包括了。您必须侦听脚本onload事件,然后调用所需的函数。

<script id="paypal_js"></script> // how do I set the src???
  <script>
  $(document).ready(function(){
    var src = 'https://www.paypal.com/sdk/js?client-id=my-client-id&email=';
    var scriptTag = document.getElementById('paypal_js')
    scriptTag.src = src+<?= email ?>;

    scriptTag.onload = function() {
      paypal.Buttons({
        createSubscription: function(data, actions) {
          return actions.subscription.create({'plan_id': 'P-my-plan'});
        },
        onApprove: function(data, actions) {
          google.script.host.close();
        }
      }).render('#paypal-button-container');
    }
  });
  </script>
//如何设置src???
$(文档).ready(函数(){
var src='1〕https://www.paypal.com/sdk/js?client-id=我的客户id和电子邮件=';
var scriptTag=document.getElementById('paypal_js')
scriptTag.src=src+;
scriptTag.onload=函数(){
贝宝,按钮({
createSubscription:函数(数据、操作){
返回actions.subscription.create({'plan_id':'P-my-plan'});
},
onApprove:功能(数据、操作){
google.script.host.close();
}
}).render(“#贝宝按钮容器”);
}
});