代码之家  ›  专栏  ›  技术社区  ›  foufrix

用javascript附加<script>内容(登录zendesk)

  •  1
  • foufrix  · 技术社区  · 7 年前

    我有一个react应用程序,我在其中附加了一个zendesk脚本,只有在某种类型的用户登录时才会显示。

    我还想使用Zendesk建议的自动登录: https://developer.zendesk.com/embeddables/docs/widget/api#content =>大小标识

    我不知道怎么做。这是我的plugin zendesk代码,我需要在后面附加它:

    // Specify which user can have Zendesk
            if (this.props.user.organisation === "organisationName") {
                // Start of Zendesk Chat Script
                const script = document.createElement("script");
    
                script.src = "myzendesklink";
                script.async = true;
                script.id = "zendesk"
    
                console.log()
                document.body.appendChild(script);
                //Something here for login
                const scriptLogin = document.createElement("script");
    
                document.body.appendChild(scriptLogin);
                // End of Zendesk Chat Script
            }
    

    以及我要附加的代码:

    <script>
      zE(function() {
        zE.identify({
          name: 'John Citizen',
          email: 'john@example.com',
          organization: 'VIP'
        });
      });
    </script>
    

    第一部分是好的和工作,但第二部分我正在努力。

    如果有人知道如何附加这个脚本,我很高兴知道:),感谢社区

    1 回复  |  直到 7 年前
        1
  •  2
  •   rap-2-h    7 年前

    根据@cbroe的评论,你可以试试这样的方法:

    if (this.props.user.organisation === "organisationName") {
        // Start of Zendesk Chat Script
        const script = document.createElement("script");
    
        script.text = "zE(function() { \
            zE.identify({ \
                name: 'John Citizen', \
                email: 'john@example.com', \
                organization: 'VIP' \
            });
        });";
    
        document.body.appendChild(script);
    
        // ...
    }
    
    推荐文章