Solution for JS String concatenation errors in ie

Source: Internet
Author: User

I haven't used js for a long time, so I forgot many things. Recently I encountered a problem when splicing strings with js. No matter how it is spliced, I reported an error in ie, which is very depressing.

After a day, I picked up the code and looked at it. Suddenly I remembered the concatenation of character string escape characters in java, and I remembered that js also had this stuff.

It turns out that:
Copy codeThe Code is as follows:
Tr + = "<td> <a href = 'javascript: void (0); 'onclick = 'confirmdeloneinfo ('" + url2 + "', '" + obj. title + "');'> ";

In the editor, no error is reported. If you click onclick in the browser, no error is reported. After reading the script, the string is completely changed.

After modification:
Copy codeThe Code is as follows:
Tr + = "<td> <a href = 'javascript: void (0); 'onclick = 'confirmdeloneinfo (\" "+ url2 + "\", \ "" + obj. title + "\"); '>
No problem.

Knowledge summary:

The effect of double quotation marks in Javascript is the same, but if your string contains single quotation marks, double quotation marks should be used outside. If your string contains double quotation marks, they should be enclosed in single quotation marks, in this way, no escape characters are needed.

The output result is as follows:
Copy codeThe Code is as follows:
Onclick = 'confirmdeloneinfo ("http ://...? Type = 1 & nodeId = 11 "," Test 1 ");'

It can be seen that in html, if single quotes are used outside, double quotation marks are used. Otherwise, IE reports an error, so Escape Character \ "is used to convert double quotation marks. When dynamically generating html elements, pay special attention to the problem of single double quotation marks. Otherwise, it will be a headache.

A little bit of knowledge, please correct me ....

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.