How to solve the problem that the Js parameter value contains single or double quotation marks

Source: Internet
Author: User

<Script type = "text/javascript">
Function Display (LoginEmail, UserName, ID ){
Alert (LoginEmail );
}
</Script>

1. Passing parameters using single quotes:
<A href = "javascript: Display ('abc @ abc.com ', 'aleax xie', '000000')"> Test1 </a>

If one of the parameters contains single quotes, this method will fail.
<A href = "javascript: Display ('abc @ a' bc. com ', 'aleax xie', '000000')"> Test1 </a>


Improvement: \ 'and OK are added with single quotes.
<A href = "javascript: Display ('abc @ a \ 'bc. com', 'aleax xie', '000000')"> Test </a>

2. The problem of single quotes is solved above, but if there is a problem with double quotes in the value
<A href = "javascript: Display ('abc @ a \ 'bc". com ', 'aleax xie', '000000') "> Test </a>

Improvement: Onclick is used, and \ "is added to double quotation marks. You do not need to process single quotation marks, and" or "is not added to JS methods.
<A href = "javascript: void (0)" onclick = Display ("abc @ \" AB 'C. com "," Aleax Xie "," 10101239393 ")> Test3 </a>

This solves the single quotation marks and double quotation marks in Js parameters. However, there are restrictions: there must be no spaces between parameters and parameters, and no spaces in parameter values. If there is a space, it is replaced.

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.