0

I am passing value dynamically to javascript function.

I am retrieving data from database and filling to javascript function, it does not have a static binding.

share_it(data_from_mysql_database);

like

share_it('value from mysql database');

Some times value contain a single quote (').

like: share_it(' Essentially you'll have to have a good academic history ');

So function call gives error that:

Uncaught SyntaxError: Unexpected identifier 
2
  • 2
    So what is your question? Commented Jul 26, 2014 at 5:36
  • before passing the string into function simply replace the ' with \' using regex Commented Jul 26, 2014 at 5:41

4 Answers 4

2

You can use the \ character to escape such characters:

share_it(' Essentially you\'ll have to have a good past academic ');

Or, you can switch to using double quotes if you know you will need to embed a single quote character:

share_it(" Essentially you'll have to have a good past academic ");
Sign up to request clarification or add additional context in comments.

2 Comments

i am passing value dynamically from database
@HiteshModha If you mean that you are building JavaScript code dynamically, then you will need to process the string yourself to add the required escape characters. How you accomplish this depends on the language you are using to build the JavaScript.
2

You can freely switch between double " and single ' quotes where you need the other in a literal string:

share_it(" Essentially you'll have to have a good past academic ");

Only in cases where you need both, you need to escape the repeating character:

share_it(" Essentially you'll have to have a good \"past\" academic ");

Comments

1

You can also replace the ' in the string with &#39.

Comments

-1

You ought to be converting special chars on the upstream rather than the downstream. Converting it on the upstream saves time later when an inexperienced developer does not care to escape the data on the downstream when sent to the client. Since you have not properly converted the data on the upstream, you have no choice. You should escape it.

share_it(escape(data_from_mysql_database));

Example"

> escape("You're awesome");
'You%27re%20awesome'
> 

2 Comments

Are you suggesting that the DB shouldn't store apostrophes or single quotes in case someone wants to output some data as a JS string literal? Also, what's with the %xx escaping in the JS string? Shouldn't you escape it with a \ instead?
The escape function is meant for encoding url parameter values. It's not meant for turning raw text into JS string literals. With this function, his code will run but it won't work properly. The proper solution is to replace all ' or " with \' or \". Yes it needs to be done "manually" whenever he writes raw data to JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.