How do I escape a string inside JavaScript code inside an onClick handler?

前端 未结 13 895
半阙折子戏
半阙折子戏 2020-11-28 03:44

Maybe I\'m just thinking about this too hard, but I\'m having a problem figuring out what escaping to use on a string in some JavaScript code inside a link\'s onClick handle

13条回答
  •  独厮守ぢ
    2020-11-28 04:33

    Another interesting solution might be to do this:

    Select
    

    Then you can use a standard HTML-encoding on both the variables, without having to worry about the extra complication of the javascript quoting.

    Yes, this does create HTML that is strictly invalid. However, it is a valid technique, and all modern browsers support it.

    If it was my, I'd probably go with my first suggestion, and ensure the values are HTML-encoded and have single-quotes escaped.

提交回复
热议问题