Inexplicable JavaScript string line feed

Source: Internet
Author: User

Today, I read the blue ideal post and talked about a string Line Break Problem. Generally, if HTML code snippets are stored in the Code, the plus sign is used to connect strings.

Of course this will be a little troublesome, so we have the following uncommon method:

Var foo = 'xxx \
Xxx ';
The result of alert (foo) is:

Add a few spaces to indent xxxxxx:

Var foo = 'xxx \
Xxx ';
The result of alert (foo) is:

Xxx
What if I don't fold it?

Var foo = 'xxx \ XXX ';
Alert (foo) returns a syntax error:

SyntaxError: malformed hexadecimal character escape sequence. In fact, this bar is a JavaScript escape character. Using \ + carriage return is equivalent to escaping the carriage return character in the string, so the pop-up is a concatenation string.

Considering that JavaScript code is usually compressed during deployment, it is too dangerous to use \, so use it with caution.

We recommend that you use the following methods to wrap strings:
1. Connect with the plus sign;
2. Data is pushed row by row and finally joined;
3. Use script tags to store HTML clips

Source: mango xiaozhan; Original: Click to View Details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.