


JavaScript has many little tricks to make programming easier. One of them is the eval() function, which treats a string as a JavaScript expression and executes it. The following is its description
Eval function
Function: first interpret the Javascript code, and then execute it
Usage: Eval(codeString)
codeString is a string containing Javascript statements, used after eval Compiled by Javascript engine.
A small example:
[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute ]
If you run this eval program, you will see that the string "2 3" is actually executed in JavaScript. So when you set the value of the_evaled_answer to eval("2 3"), JavaScript will understand and return the sum of 2 and 3 to the_evaled_answer.
This may seem a bit silly, but it can actually do very interesting things. For example, using eval you can create functions directly based on user input. This allows the program to change itself based on time or user input, and by applying inferences you can get amazing results.
In practice, eval is rarely used, but maybe you have seen someone using eval to get hard-to-index objects. One of the problems with the Document Object Model (DOM) is that sometimes it's a pain to get the object you ask for. For example, here is a function that asks the user which image to transform: To transform which image you can use the following function:
function swapOne()
{
var the_image = prompt("change parrot or cheese","");
var the_image_object;
if (the_image == "parrot")
{
the_image_object = window.document.parrot;
}
else
{
the_image_object = window.document.cheese;
}
the_image_object.src = "ant.gif";
}
along with these image tags:


Please pay attention to a few lines like this:
the_image_object = window.document.parrot;
It assigns an image object to a variable. Although it looks a bit strange, it is grammatically correct. But what happens when you have 100 images instead of two? You have to write a lot of if-then-else statements, if only it could be like this:
function swapTwo()
{
var the_image = prompt("change parrot or cheese","");
window.document.the_image.src = " ant.gif";
}
Unfortunately, JavaScript will look for an image named the_image instead of "cheese" or "parrot" as you expected, so you get Error message: "Never heard of an object named the_image".
Fortunately, eval can help you get the object you want.
function simpleSwap()
{
var the_image = prompt("change parrot or cheese","");
var the_image_name = "window.document." the_image;
var the_image_object = eval(the_image_name);
the_image_object.src = "ant.gif";
}
If the user fills in "parrot" in the prompt box, a string is created in the second line, window.document.parrot. Then the third line containing eval means: "Give me the object window.document .parrot" - that is, the image object you want. Once you obtain the image object, you can set its src attribute to ant.gif. A little scared? No need. It's actually quite useful and people use it a lot.

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates creating dynamic page boxes loaded via AJAX, enabling instant refresh without full page reloads. It leverages jQuery and JavaScript. Think of it as a custom Facebook-style content box loader. Key Concepts: AJAX and jQuery

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

This JavaScript library leverages the window.name property to manage session data without relying on cookies. It offers a robust solution for storing and retrieving session variables across browsers. The library provides three core methods: Session

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
