How to pass special characters based on javascript _ javascript skills

Source: Internet
Author: User
In JavaScript, a backslash is used to add special characters to a text string. The following article describes how to pass special characters in javascript, if you are interested in passing special characters in JavaScript, let's take a look at it. Before you get on the right track, we will introduce you to special JavaScript characters.

You can use a backslash in JavaScript to add special characters to a text string.

Insert special characters

A backslash is used to insert ellipsis, line breaks, quotation marks, and other special characters into a text string.

See the following JavaScript code:

var txt="We are the so-called "Vikings" from the north."document.write(txt)

In JavaScript, strings start or end with single or double quotation marks. This means that the above string will be truncated to: We are the so-called.
To solve this problem, you must add a backslash (\) before the quotation marks in "Viking (\). In this way, each double quotation mark can be converted to a literal string.

var txt="We are the so-called \"Vikings\" from the north."document.write(txt)

Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.

This is another example:

document.write ("You \& me are singing!") 

The preceding example generates the following output:

You & me are singing!

The following table lists other special characters that can be added to a text string using a backslash:

Code Output
\' Single quotes
\" Double quotation marks
\& And number
\\ Backslash
\ N Line Break
\ R Carriage Return
\ T Tab
\ B Escape Character
\ F Page feed

Background:

When I was doing a task today, when I passed a long string of characters using jquery ajax, the verification in the background was always unsuccessful. When I struggled (the string was randomly generated and specialized ). after checking the morning, it turns out that there is a + sign in the source string that I generated. When passing the + sign in js, it will be understood as the connection character land. When it arrives at the background, it will automatically change the + sign to a space, therefore, the background strings are different from those generated at the foreground.

Cause:

Special characters are automatically parsed after js, such as the connector +, space, and variable connector. When the Server accepts data, the data in the future is not displayed.

Solution:

1. Place the characters in form and submit the form to the server using js.
2. Replace special characters with hexadecimal characters. Some special characters correspond to hexadecimal characters:

+ Space / ? % & = #
% 2b % 20 % 2f % 3f % 25 % 26 & 3d % 23

Str = str. replace (/\ +/g, % 2b); replace the "+" with the hexadecimal notation.

3. Use the encodeuricomponent () function.

This method does not encode ascii letters and numbers, and does not encode these ascii punctuation marks :-_.! ~ *'().
Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate uri components) are all replaced by one or more hexadecimal sequence.

JQuery ajax Special Character Parameters

A problem encountered during ajax login. When the input parameter contains special characters, such as "$. The parameter is transmitted incorrectly.

$.ajax({  url: '/user/login.ydd',  type:'post',  data:'name=abce&password=abcd&pwd',  success: function(data){  }})

I want to input a user with the username abc and password abcd & pwd to log on. However, the parameter obtained in the background is passed as a parameter by password = abcd. & in this special example, pwd is separated and parsed as another parameter.

Solution: in this case, ajax needs another method to pass parameters.

$.ajax({  url: '/user/login.ydd',  type:'post',  data:{'name':'abce','password':'abcd&pwd'},  success: function(data){   }})

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.