Is there a way to bypass using eval in this particular instance with JavaScript?

My challenge involves working with a customer's specific task in Javascript Framework.

Here is the HTML code snippet:

<div data-foo="(#a = #b)">...loading</div>

When I retrieve the data-*-Attribute in Javascript, it comes as a string:

foo = '(#a = #b)'

An ajax call returns the following response:

#a=1, #b=1

The tags are then replaced with the values from the ajax call (including the operator):

foo = '(1 == 1)'

Subsequently, the foo string is evaluated using eval();

result = eval(foo)  // true

Is there a way to avoid using eval()? My task involves evaluating strings like '(0 == 1)' or '((0 == 0) && (1 == 0))', and I do not have control over the server response. I am looking for a secure method to evaluate these strings to either true or false.

EDIT:

The possible strings include:

'(0 == 0)'
'(0 == 1)'
'(0 > 5)'
'(117 > 0)'
'((0 == 1) && (11 == 11))'
'((0 == 1) || (0 == 0))'
'(((0 < 1) || (0 == 0) ) && (33 != 11))'
...and so on!

The expected result will always be true or false.

Answer №1

If you're looking for solutions to your issue, check out the following helpful resources:

There are countless other options available as well. Try searching for "javascript expression parser".

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The call stack is overflowing due to excessive use of async.queue, causing a

I'm encountering an issue where I am receiving a "stack size exceeded" error when running the code snippet below. The code works perfectly fine with fewer items (tested up to 1000), but as soon as I increase the number of items, this error occurs cons ...

Using the symbol for pi in a JavaScript program rather than its numerical value, while still performing calculations with the numerical value behind the scenes

I am working on a calculator project where I want the symbol for pi to be displayed instead of the numerical value. Here is the relevant function: function Pi(pi) { document.getElementById('resultArea').innerHTML += pi; eval(document.ge ...

Icon component with a generic style

I have been working on styling my react component using styled-components. My goal is to create an icon component that can be easily customized by changing properties such as size, color, and the actual icon name. While I have been successful in adjusting ...

Tap here for supplementary HTML

My HTML code looks like this: <a class="link">text</a> <div class="items"></div> (function($) { var link = $('.link'); link.click(function(){ alert('it works!'); }); $('.items').each(fun ...

The appearance of the logout button may differ depending on the web browser being used

I have implemented a straightforward logout button using the following code: <li><a href="http://localhost:8666/web1/profile/mainpage/logout.php" onclick="return confirm('Are you sure to logout?');">Log Out</a>&l ...

Node.js routing currently lacks the ability to easily verify the presence of a JWT token for every route

I have a node application where, upon routing to ('/login') with valid credentials, I generate a JWT token with an expiry time and direct the user to the next page. If the user tries to access any other route directly (e.g., '/home') af ...

Trigger an Angular controller within Foundation's reveal modal

Trying to implement a form using foundation's reveal modal, I want to incorporate an angular controller within the form for ajax form submission instead of the default post/refresh behavior. This is my primary view: <html lang="es" ng-app="crm"&g ...

Using jQuery to show and hide elements on a webpage

One issue I'm facing is changing the content on a page depending on the clicked link. The problem arises when the displayed content for one link persists even after clicking another link, despite setting it to not display when another link is clicked. ...

Troubleshooting problem: Unable to restrict table selections - issue with Material UI table in React

I seem to be overlooking the simple solution here. Currently, I have a ternary on my table. If the length of the selected array is greater than a certain number, a table with disabled checkboxes is rendered. I also implement a different handleClick functio ...

Utilizing Laravel's eloquent capabilities to perform a dynamic join operation via

Hello! I'm looking for information on how to use Laravel Eloquent join in an AJAX success function. Can anyone help? Here is the User Model: <?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation& ...

The configuration for the 'proxy' is all good and properly set

image description While trying to install the npm package "pdf-node", I encountered an error: Error: ENOTFOUND - getaddrinfo failed for . This seems to be a network connectivity issue. To resolve this error, make sure your network settings are configure ...

Can an UpdatePanel in ASP.Net be triggered by a control within a UserControl?

In my project, I have developed a Footer UserControl where I dynamically insert controls based on certain conditions. Some of these controls are intended to trigger an UpdatePanel on the same page. The code behind looks like this: private void SetupFoote ...

Is the function failing to detect the updated variable because it is not declared as a global variable?

I have created a quiz with 7 select boxes and a submit button, aiming to display a warning error if the select boxes are not changed or show the score if they are changed. I initialized a variable called 'changed' as false. When a select box is ...

Add a jQuery script to the admin panel of a custom WordPress plugin for sending emails through ajax

I've been working on integrating a form into an admin page on WordPress. The goal is to allow users to input their email address and trigger an email to be sent to that address. To achieve this, I'm utilizing a jQuery Ajax function to transmit th ...

Tooltip displacement on the webpage

I'm encountering an issue with the placement of tooltips in my buttons. The problem is specifically with the last, right-most button tooltip - it seems to be displaying on the left side of the page instead of below the button as intended. Check out t ...

Returning false will not terminate the each loop in cypress testing

Here is the code snippet I am working with: cy.get('[data-testid="userRecords"]') .each((record) => { if (record.find('[data-testid="delete"]').is(":enabled")) { cy.wrap(record).find(& ...

What methods can I use to identify when a browser autofills a saved password during a web login process?

My website has a feature that activates the login button when a user enters their username and password. However, an issue arises when the browser autofills this information, as the login button does not get enabled in such cases. Is there a way to use J ...

Sending a JavaScript object as a prop in a React component

Currently, I am enrolled in a React course that requires us to pass a single JavaScript object as props to a React application. Here's the code snippet I have been working on: import React from 'react'; import ReactDOM from 'react-dom& ...

This issue with ng-include not functioning properly in Chrome but working fine in Firefox

My code is running only in Firefox and not working in Chrome. HTML Code <div ng-controller="myController1"> Select View: <select ng-model="employeeview"> <option value="1.html">1</option> < ...

Leveraging React hooks to combine an array and an object within an array

Struggling to incorporate an array and an object into another array. This is the setup in my constructor: const [dashboard, setDashboard] = useState({ loading: true, data: [], address: '' }) This is how I envision the final data structure: { ...