JS& 1个片段中的条件语句

时间:2016-01-08 15:15:22

标签: javascript php

我正在使用我们网站的两种不同语言为我们值得信赖的电子商务徽章实施代码。

我们有2种语言ergo 2"不同"网站和ID与他们一起使用,代码如下,我希望var _tsid根据网站的语言而改变。

我有一个正常运行的PHP脚本,但问题是我只能在我们的CMS上的这个代码在一个javascript框中运行php脚本没有效果

基本上我有2个长30digit代码,它们是我们的客户端ID来代表我们的徽章,所以它只需要使用JS就可以互换。任何帮助是极大的赞赏!我尝试使用jsfiddle,但是使用console.log等尝试获取打印件或者某些东西没有正常工作所以我只是坚持在这里,希望看看是否有人可以帮我找到解决方案:)

运行PHP脚本+ JS

<?php 
if ($ts_lang == "en" || $ts_lang == "fr"){
      if ($ts_lang == "en"){ 
      $tsid = "Code1";
      }elseif($ts_lang == "fr"){
      $tsid = "Code2";
      }?>
      <script type="text/javascript">
          (function () { 
          var _tsid = '<?php echo $tsid ;?>'; 
          _tsConfig = { 
              'yOffset': '0', //offset from page bottom
              'variant': 'reviews' //text, default, small, reviews
          };
          var _ts = document.createElement('script');
          _ts.type = 'text/javascript'; 
          _ts.async = true; 
          _ts.charset = 'utf-8'; 
          _ts.src = '//widgets.trustedshops.com/js/' + _tsid + '.js'; 
          var __ts = document.getElementsByTagName('script')[0];
          __ts.parentNode.insertBefore(_ts, __ts);
          })();
      </script>
<?php } ?>

我尝试使用JS if / else语句

<script type = "text/javascript" >

    var sitelang = en;
    var _tsid;
    if (sitelang == en) {
      _tsid = "Code1";
    } else {
      _tsid = "Code2";
    }

(function() {
    _tsConfig = {
      'yOffset': '0', /* offset from page bottom */
      'variant': 'reviews', /* text, default, small, reviews, custom, custom_reviews */
      'customElementId': '', /* required for variants custom and custom_reviews */
      'trustcardDirection': '', /* for custom variants: topRight, topLeft, bottomRight, bottomLeft */
      'customBadgeWidth': '', /* for custom variants: 40 - 90 (in pixels) */
      'customBadgeHeight': '', /* for custom variants: 40 - 90 (in pixels) */
      'disableResponsive': 'false', /* deactivate responsive behaviour */
      'disableTrustbadge': 'false', /* deactivate trustbadge */
      'trustCardTrigger': 'mouseenter' /* set to 'click' if you want the trustcard to be opened on click instead */
    };
    var _ts = document.createElement('script');
    _ts.type = 'text/javascript';
    _ts.charset = 'utf-8';
    _ts.async = true;
    _ts.src = '//widgets.trustedshops.com/js/' + _tsid + '.js';
    var __ts = document.getElementsByTagName('script')[0];
    __ts.parentNode.insertBefore(_ts, __ts);
  })(); 

</script>

这是当前正在运行的代码,您可以在www.showstyle.lu右下方看到“信任商店”徽章

<script type="text/javascript">
  (function () { 
    var _tsid = 'Code1'; 
    _tsConfig = { 
      'yOffset': '0', /* offset from page bottom */
      'variant': 'reviews', /* text, default, small, reviews, custom, custom_reviews */
      'customElementId': '', /* required for variants custom and custom_reviews */
      'trustcardDirection': '', /* for custom variants: topRight, topLeft, bottomRight, 

bottomLeft */
      'customBadgeWidth': '', /* for custom variants: 40 - 90 (in pixels) */
      'customBadgeHeight': '', /* for custom variants: 40 - 90 (in pixels) */
      'disableResponsive': 'false', /* deactivate responsive behaviour */
      'disableTrustbadge': 'false', /* deactivate trustbadge */
      'trustCardTrigger': 'mouseenter' /* set to 'click' if you want the trustcard to be opened on click instead */
    };
    var _ts = document.createElement('script');
    _ts.type = 'text/javascript'; 
    _ts.charset = 'utf-8'; 
    _ts.async = true; 
    _ts.src = '//widgets.trustedshops.com/js/' + _tsid + '.js'; 
    var __ts = document.getElementsByTagName('script')[0];
    __ts.parentNode.insertBefore(_ts, __ts);
  })();
</script>

1 个答案:

答案 0 :(得分:0)

我不知道我是否理解但我认为你需要检测sitelang是“en”还是“fr”,所以你可以使用document.location.pathname,因为你有http://www.showstyle.lu/en/和{{3 }}

尝试:

<script type = "text/javascript" >
    var sitelang=document.location.pathname;
    //var sitelang = en;
    var _tsid;
    if (sitelang == "/en/") {
      _tsid = "Code1";
    } else {//"/fr/"
      _tsid = "Code2";
    }

(function() {
    _tsConfig = {
      'yOffset': '0', /* offset from page bottom */
      'variant': 'reviews', /* text, default, small, reviews, custom, custom_reviews */
      'customElementId': '', /* required for variants custom and custom_reviews */
      'trustcardDirection': '', /* for custom variants: topRight, topLeft, bottomRight, bottomLeft */
      'customBadgeWidth': '', /* for custom variants: 40 - 90 (in pixels) */
      'customBadgeHeight': '', /* for custom variants: 40 - 90 (in pixels) */
      'disableResponsive': 'false', /* deactivate responsive behaviour */
      'disableTrustbadge': 'false', /* deactivate trustbadge */
      'trustCardTrigger': 'mouseenter' /* set to 'click' if you want the trustcard to be opened on click instead */
    };
    var _ts = document.createElement('script');
    _ts.type = 'text/javascript';
    _ts.charset = 'utf-8';
    _ts.async = true;
    _ts.src = '//widgets.trustedshops.com/js/' + _tsid + '.js';
    var __ts = document.getElementsByTagName('script')[0];
    __ts.parentNode.insertBefore(_ts, __ts);
  })(); 

</script>