Jquery ajax eval processing returns js code

Source: Internet
Author: User
Tags eval object model

We have always used the string sharding method to process the data returned by AJAX. Today, baidu finds that eval () can be used.
For example: eval (http_request.responseText );

Eval () function

JavaScript has many tips to make programming easier.
One of them is the eval () function, which can execute a string as a JavaScript expression.
For example:

The code is as follows: Copy code
// Execution expression
Var the_unevaled_answer = "2 + 3 & Prime ;;
Var the_evaled_answer = eval ("2 + 3 & Prime ;);
Alert ("the un-evaled answer is" + the_unevaled_answer + "and the evaled answer is" + the_evaled_answer );

If you run this eval program, you will see the string "2 + 3 & Prime; actually executed in JavaScript.
So when you set the_evaled_answer to eval ("2 + 3 & Prime;), JavaScript will understand and return the sum of 2 and 3 to the_evaled_answer.
This seems a bit silly, but it can actually make interesting things. For example, you can use eval to directly create a function based on user input.
This allows the program to change the program itself based on time or user input. You can achieve amazing results by taking the opposite picture.
In practice, eval is rarely used, but you may have seen people use eval to obtain objects that are hard to index.

One of the problems with the Document Object Model (DOM) is that sometimes you need to get www.111cn.net to get the object you want is simply a pain.
For example, here is a function that asks the user which image to transform: you can use the following function to change which image:

The code is as follows: Copy code

Function swapOne (){
Var the_image = prompt ("change parrot or cheese ","");
Var the_image_object;

If (the_image = "parrot "){
The_image_object = your role doc ument. parrot;
} Else {
The_image_object = plain doc ument. cheese;
}

The_image_object.src = registrant.gif ";
}

Together with these image tags:

The code is as follows: Copy code

[Img src = "yun_qi_img/a.gif" name = "parrot"]
[Img src = "yun_qi_img/B .gif" name = "cheese"]
[Code]
Note the following statements:
[Code]
The_image_object = your role doc ument. parrot;

It applies an image object to a variable. Although it looks a bit strange, it has no syntax problems.
But what if you have 100 images instead of two images? You have to write a lot of if-then-else statements. if it works like this:

The code is as follows: Copy code

Function swapTwo (){
Var the_image = prompt ("change parrot or cheese ","");
Required parameter doc ument. the_image.src = "ant.gif ";
}

Unfortunately, JavaScript will look for images named the_image instead of the desired "cheese" or "parrot,
So you get the error message: "I have never heard of an object named the_image ".

Fortunately, eval can help you get the object you want.

The code is as follows: Copy code

Function simpleSwap (){
Var the_image = prompt ("change parrot or cheese ","");
Var the_image_name = "zookeeper doc ument." + the_image;
Var the_image_object = eval (the_image_name );
The_image_object.src = "ant.gif ";
}

If you enter "parrot" in the prompt box, create an example string in the second line, that is, the example doc ument. parrot. Contains the third part of eval.
The line means: "Give me the object named doc ument. parrot"-that is, the image object you want. Once you obtain this image object, you can
Maybe srcflood is set to ant.gif. A little scared? No. In fact, this is quite useful and is often used by people.
We often go to the Eval function in the middle of Javascript,
Some people think this function is very strange. It can make some strings very powerful.
This function is used when we need to convert a common string into a specific object.

The eval function evaluates a string used as a numeric expression. Its syntax is:

Eval (expr)

Expr is a string parameter evaluated. If the string is an expression, eval evaluates the value of this expression. If this parameter represents one or more JavaScript statements, eval executes these statements. The eval function can be used to convert a date from a format (always string) to a numeric expression or number.

 

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.