简体   繁体   English

JS if 语句中的 PHP else if 语句条件为真时出错。

[英]Error when PHP else if statement condition is true inside the JS if statement.

I have combined some jQuery and PHP within my function.我在我的函数中结合了一些 jQuery 和 PHP。 Google Chrome renders the code correctly and the result is as expected, however, Opera or Firefox are giving me Uncaught SyntaxError: missing ) after argument list error.谷歌浏览器正确呈现代码,结果如预期,但是,Opera 或 Firefox Uncaught SyntaxError: missing ) after argument list错误Uncaught SyntaxError: missing ) after argument list给我Uncaught SyntaxError: missing ) after argument list

The PHP part is just checking if the customer is logged in to my Magento store and then prepend the correct div. PHP 部分只是检查客户是否登录到我的Magento商店,然后添加正确的 div。

PHP/JS: PHP/JS:

AddWelcomeBar();

jQuery(window).resize(function() {
        AddWelcomeBar()
});

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$customerName = $customerSession->getCustomer()->getName();
?>

function AddWelcomeBar() {
    var innerWidth = window.innerWidth;
    if (innerWidth < 850) {
        <?php   if ($customerSession->isLoggedIn()) { ?>
            var customer = "<?php echo $customerName ?>";
            var welcomebar = jQuery(".welcomebar");
            if (welcomebar.length == 0) {
                jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>Welcome," + customer + "</div>");
            <?php } else { ?>
                jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>10% OFF YOUR 1ST ORDER: CODE 'VV10'</div>");
            <?php } ?>
    } else if (innerWidth > 850) {
            jQuery(".section-item-content > .welcomebar").remove();
            jQuery(".section-item-content").show();
    }
  }
}

HTML: HTML:

<div class="section-item-content">

</div>

If I remove the last closing } , then it works on Opera and Firefox but Google chrome is saying that there is a } missing.如果我删除最后一个关闭} ,那么它适用于 Opera 和 Firefox,但谷歌浏览器说缺少} Any ideas?有任何想法吗?

Basically, fixing the problem on one browser, breaks the code on the other.基本上,在一个浏览器上修复问题,在另一个浏览器上破坏代码。

Got it!知道了!

The PHP if else when ran, closed the if (welcomebar.length == 0) { when it shouldn't do. PHP if else在运行时关闭if (welcomebar.length == 0) {当它不应该这样做时。 The first condition worked, however, the php else was messing up the JS if statement.第一个条件有效,但是,php else弄乱了 JS if 语句。

Wrapping the PHP if statement with the if(welcomebar.length == 0) { solved my problem.if(welcomebar.length == 0) {包装 PHP if 语句解决了我的问题。

Here is the working code:这是工作代码:

function AddWelcomeBar() {
            var innerWidth = window.innerWidth;
            if (innerWidth < 850) {
                var welcomebar = jQuery(".welcomebar");
                if (welcomebar.length == 0) {
                    <?php   if ($customerSession->isLoggedIn()) { ?>
                        var customer = "<?php echo $customerName ?>";
                            jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>Welcome," + customer + "</div>");
                        <?php } else { ?>
                            jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>10% OFF YOUR 1ST ORDER: CODE 'VV10'</div>");
                        <?php } ?>
                    }
            } else if (innerWidth > 850) {
                    jQuery(".section-item-content > .welcomebar").remove();
                    jQuery(".section-item-content").show();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM