?
Solved

Javascript: Using eval() with a string containing double quotes and single quotes

Posted on 2012-03-14
9
Medium Priority
?
1,130 Views
Last Modified: 2012-03-14
Hello all.

I am trying to "push" values into an array after they've been used in an eval() statement. The issue I'm running into is that with the way I'm concatenating the string, the double quotes inside the "text being evalled" is causing the eval() function to choke.

Here is an example.

Description fields is equal to -------->        This is a test, "yes", a test

eval("obj." + "description" + " = '" + $('#description').val() + "'");      

With this as an example, the " in "yes" messes things up. I've tried using " but the & then messes it up. I've also tried replacing the " with \".

Any suggestions on how to "escape" the double quote?
0
Comment
Question by:Webspeeder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 11

Expert Comment

by:SANDY_SK
ID: 37721866
have you tried replacing " with \\\" OR \\\\\"

That is escaping the the escaping character
0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 37721926
instead of
eval("obj." + "description" + " = '" + $('#description').val() + "'");
have u tried like below
eval("obj." + "description" + " = '" + $('#description').val().replace('"','\"') + "'");
0
 

Author Comment

by:Webspeeder
ID: 37721987
replacing " with \" generates this JS error in firebug.

Error: missing ; before statement
Source File: http://10.9.10.220/js/jquery2/jquery-1.6.2.min.js
Line: 38, Column: 22
Source Code:
obj.description = '9' 6" 2PC 3LBL testing 3'

Well, looking at the message closely, why would this be tied to jQuery?

var xx = htmlEncode($('#description').val());
eval("obj." + "description" + " = '" + xx + "'");

function htmlEncode(value){
  return value.replace(/"/g,'\"');
}
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:Webspeeder
ID: 37721999
When trying to "escape the escape character", I get this:

Error: missing ; before statement
Source File: http://10.9.10.220/js/jquery2/jquery-1.6.2.min.js
Line: 38, Column: 22
Source Code:
obj.description = '9' 6\\" 2PC 3LBL testing 3'
0
 

Author Comment

by:Webspeeder
ID: 37722004
Might it be the single quote messing it up?
0
 

Author Comment

by:Webspeeder
ID: 37722006
I've tried escaping both with \' and \"
0
 
LVL 11

Expert Comment

by:SANDY_SK
ID: 37722077
can you paste that portion of the code if you don't mind.
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 1000 total points
ID: 37722155
Just use
obj["description"] = $("#description").val();

Open in new window

0
 

Author Closing Comment

by:Webspeeder
ID: 37722427
That did it. Thank you very much.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question