动态html正文脚本Var没有定义?

时间:2014-11-10 19:22:48

标签: javascript html backbone.js

我正在处理亚马逊付款。

我动态插入两个脚本,一个在头部,一个在体内。但是我的文件BODY中的SCRIPT元素说变量" OffAmazonPayments"在HEAD中没有定义。有问题的变量在BODY中的脚本之前动态插入到文档的HEAD中,并且可以从控制台获得。

setupAmzn: () ->
    amazonLoginReady = document.createElement('script')
    amazonLoginReady.innerHTML = "
        window.onAmazonLoginReady = function() {
            amazon.Login.setClientId('CLIENT_ID_OBFU');
        }
    ";

    amazonWidgetJS = document.createElement('script');
    amazonWidgetJS.src = "https://static-na.payments-amazon.com/OffAmazonPayments/us/sandbox/js/Widgets.js";

    s = document.getElementsByTagName('script')[0]
    s.parentNode.insertBefore(amazonLoginReady, s)
    s.parentNode.insertBefore(amazonWidgetJS, s)

renderAmzn: () ->
    console.log("adding the login and pay button scripts")
    @setupAmzn()

    #create Login and Pay button

    payButtonContainer = document.createElement("div");
    payButtonContainer.setAttribute("class", "payButtonContainer");

    payButton = document.createElement("div");
    payButton.setAttribute("id", "AmazonPayButton"); 

    payButtonScript = document.createElement('script');
    payButtonScript.innerHTML = 'console.log(typeof OffAmazonPayments === "undefined")'; 

    # !!!!OffAmazonPayments is NOT in scope here 

    payButtonContainer.appendChild payButton
    payButtonContainer.appendChild payButtonScript

    proceed = @$('.proceed');
    proceed.append(payButtonContainer);

renderAmzn()在我的Backbone.view的render函数中调用。 我对变量范围的错误是什么?谢谢。

0 个答案:

没有答案