duanjue6584 2014-07-26 05:33 采纳率: 100%
浏览 92
已采纳

在javascript函数调用中由于单引号导致的错误

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 
  • 写回答

4条回答 默认 最新

  • dsbqfrr098575666 2014-07-26 05:42
    关注

    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'
    > 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?