How to resolve the apostrophe inside javascript

javascript escape quotes function
apostrophe issue in javascript
how to deal with apostrophe in javascript
javascript escape single quote in string variable
javascript escape double quotes in string
javascript string
javascript escape characters
javascript variable in string

How to use correctly the ' inside this javascript.

i try this different solutions but it does not work

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

There the complete code.

     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>
  ';
        }
      }

The only thing you need to use is json_encode(). This will deal with all possible escaping issues that might arise. The resulting value will be quoted already, there's no need to use quotes in the JavaScript code.

When you're working with long blocks of text, using a heredoc makes things much easier to read, and I would recommend putting your PHP values into variables to ensure they're more separate from your JavaScript code.

$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;
    }
}

How To Work with Strings in JavaScript, In JavaScript, there are three ways to write a string — they can be written inside single quotes ( ' ' ), double quotes ( " " ), or backticks ( ` ` ). In JavaScript, the apostrophe (') is a string delimiter. That is, it marks the beginning or end of a text string. If you include an apostrophe inside a string

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
    '\'';

In your script your combining javascript and php in the wrong way. You need to echo php variables, other wise php will not parse the assuring. To do that, I am using the php shorthand which is equivalent to

<?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>

Apostrophe code in JS, JavaScript allows single quotes or double quotes for a string, so a simple way to avoid the confusion is to use the other type than the contents of  This is not a valid HTML character entity reference. It is an XML character entity reference. While Firefox and Chrome, at least, will render the above as an apostrophe in an HTML document, Internet Explorer will not. And it is following the standard when it refuses to do so. You may escape an apostrophe in HTML using &#39;

This can all be avoided by using double quotes (" ... ") for $output.

Specifically, $output .= " ... ";

Strings, You'll need to use opposite quotation marks inside and outside. That means strings containing single quotes need to use double quotes and strings containing  Can also be a Promise or a thenable to resolve. Return value. A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object. Description. The static Promise.resolve function returns a Promise that is resolved. Examples Using the static Promise.resolve method

How do I deal with apostrophe in user name while passing variable , I pass a variable of Current User to a javascript page. It works fine unless the name includes an apostrophe (O'Neil). The formula includes: "var  Alternatively, escape the apostrophe: theAnchorText = 'I\'m home'; The backslash tells JavaScript (this has nothing to do with jQuery, by the way) that the next character should be interpreted as "special". In this case, an apostrophe after a backslash means to use a literal apostrophe but not to end the string.

The real difference between 'single quotes' and “double quotes” in , Use ESLint for consistency. If consistent quote style in JavaScript is important to you, like it is to the engineers at Airbnb, then it is easy to fix  In simple terms, inside a then handler function: A) When x is a value (number, string, etc): return x is equivalent to return Promise.resolve(x) throw x is equivalent to return Promise.reject(x) B) When x is a Promise that is already settled (not pending anymore): return x is equivalent to return Promise.resolve(x), if the Promise was already resolved.

JavaScript: The Definitive Guide, Since the apostrophe is the same as the single-quote character, you must use the backslash character (\) to escape any apostrophes that appear in  One method, common in newspapers and magazines, is to add an apostrophe + s ('s) to common nouns ending in s, but only a stand-alone apostrophe to proper nouns ending in s. Examples: the class's hours Mr. Jones' golf clubs the canvas's size Texas' weather. Care must be taken to place the apostrophe outside the word in question.

Comments
  • You should use json_encode()
  • Did you even test this code? Quotes are missing on line 1, closing parentheses are missing on line 3, closing single quote is missing on last line, I can go on.
  • @DerekPollard, does'nt work, Update for the code
  • I no choice to use $output, it's a simple example, but I have more. I put above all the code.
  • Edwin Dijas Chiwona has given a better answer