如何解决JavaScript中的撇号

时间:2018-11-19 21:28:58

标签: javascript php

如何正确使用此javascript中的'。

我尝试了这种不同的解决方案,但是它不起作用

     $introduction = 'Hello, it\'s chrismas'.
     $introduction = str_replace(''', '', $introduction);
     $introduction = json_encode($introduction);
     $introduction = stripslashes(htmlspecialchars_decode($introduction, ENT_QUOTE);

有完整的代码。

     if (MODULE_WEB_PUSH_SPECIAL == 'True') {
        $special_products = $this->getSpecialsProducts();

        if (!empty($special_products) && $_SESSION['SpecialsProducts'] === false) {
          $message_products_special = $special_products;
          $_SESSION['SpecialsProducts'] = true;

          $special_link = CLICSHOPPING::link(null, 'Products&Specials');

          $output .= '
<script>
Push.create(\'' . $introduction . '\', {
    body: \'' . $message_products_special . '\',
    icon: \'sources/images/logos/others/favicon.png\',
    timeout: 8000,               // Timeout before notification closes automatically.
    vibrate: [100, 100, 100],    // An array of vibration pulses for mobile devices.
    onClick: function() {
        window.location = "' . $special_link . '";
    }  
});
</script>
  ';
        }
      }

3 个答案:

答案 0 :(得分:0)

Escaping quotes in javascript.
Using double qoutes allows you to have a single quote inside the double quote.
    "'"
Using backslash prevents the next special character. Ie
    '\'';

在脚本中,您以错误的方式组合了javascript和php。您需要回显php变量,其他明智的php不会解析保证。为此,我使用的是

的php速记
<?php $introduction = 'Hello, it\'s chrismas'.
     $introduction = str_replace('&#039;', '', $introduction);
     $introduction = json_encode($introduction);
     $introduction = stripslashes(htmlspecialchars_decode($introduction, ENT_QUOTE); ?>



  <script>
  Push.create('<?=$introduction?>', {
      body: '<?=$message?>',
      icon: 'images/logos/others/favicon.png',
      timeout: 8000,               // Timeout before notification closes automatically.
      vibrate: [100, 100, 100],    // An array of vibration pulses for mobile devices.
  });
  </script>

答案 1 :(得分:0)

这可以通过在" ... "中使用双引号($output)来避免。

具体来说,$output .= " ... ";

答案 2 :(得分:-1)

您唯一需要使用的是json_encode()。这将处理可能出现的所有可能的转义问题。结果值已经被引用了,不需要在JavaScript代码中使用引号。

在处理较长的文本块时,使用heredoc会使内容更容易阅读,我建议您将PHP值放入变量中,以确保它们与JavaScript代码更独立。

$introduction = "Hello, it's Christmas";
if (MODULE_WEB_PUSH_SPECIAL == 'True') {
    $special_products = $this->getSpecialsProducts();

    if (!empty($special_products) && $_SESSION['SpecialsProducts'] === false) {
        $message_products_special = $special_products;
        $_SESSION['SpecialsProducts'] = true;

        $special_link = CLICSHOPPING::link(null, 'Products&Specials');

        $introduction = json_encode($introduction);
        $message_products_special = json_encode($message_products_special);
        $special_link = json_encode($special_link);

        $output = <<< JS
<script>
/* Begin PHP values */
var introduction = $introduction;
var message_products_special = $message_products_special;
var special_link = $special_link;
/* End PHP values */

Push.create(introduction, {
    body: message_products_special,
    icon: 'sources/images/logos/others/favicon.png',
    timeout: 8000,               // Timeout before notification closes automatically.
    vibrate: [100, 100, 100],    // An array of vibration pulses for mobile devices.
    onClick: function() {
        window.location = special_link;
    }  
});
</script>
JS;
    }
}