Solved

Escape in eval function on Single Quotation

Posted on 2006-07-07
7
708 Views
Last Modified: 2008-03-10
Try these two sample scripts:

This works:
<script>
var boys = 'He said \'howdy\', I said \'hi\'.' // nesting single quotes within single quotes
</script>
<a href="javascript:alert(boys)">Show me the string</a>

This does not work:
<script>
var boys = eval("'He said \'howdy\', I said \'hi\'.'") // nesting single quotes within single quotes
</script>
<a href="javascript:alert(boys)">Show me the string</a>

Why is this? Does not eval handle escape singel character?
And what should I put instead of \' in the eval function to make it work?

Thank you,
Best regards,
Marius
0
Comment
Question by:traelnes
7 Comments
 
LVL 2

Expert Comment

by:vikrant4u2
ID: 17057510
Hi Marius,

try this
<script>
var val='He said \'howdy\', I said \'hi\'.';
var boys = eval("val"); // nesting single quotes within single quotes
</script>
<a href="javascript:alert(boys)">Show me the string</a>

regards,
vikrant
0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 17057519
Hello traelnes,

because \' itself is eval again and after eval("'") you've get single quote that have to be evaled again and you get unterminated string constant.

Why you have to eval this? just concatenate...

HTH

I
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 17057693
Escape chars has to be escaped in eval():

       var boys = eval("'He said \\'howdy\\', I said \\'hi\\'.'");

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:traelnes
ID: 17057957
Thank you Zvonko for this. It works.

Just curious about what ivostoykov mean by concatenate?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17058099
You are welcome.
Let us wait and see what concatenate trick comes along ;-)
0
 

Author Comment

by:traelnes
ID: 17066600
No Concentate. Ok, Zvonko, you are the winner!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17067556
:)
Thanks.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

829 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