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

前端 未结 13 844
半阙折子戏
半阙折子戏 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:21

    In JavaScript you can encode single quotes as "\x27" and double quotes as "\x22". Therefore, with this method you can, once you're inside the (double or single) quotes of a JavaScript string literal, use the \x27 \x22 with impunity without fear of any embedded quotes "breaking out" of your string.

    \xXX is for chars < 127, and \uXXXX for Unicode, so armed with this knowledge you can create a robust JSEncode function for all characters that are out of the usual whitelist.

    For example,

    <a href="#" onclick="SelectSurveyItem('<% JSEncode(itemid) %>', '<% JSEncode(itemname) %>'); return false;">Select</a>
    
    0 讨论(0)
  • 2020-11-28 04:24

    Is the answers here that you can't escape quotes using JavaScript and that you need to start with escaped strings.

    Therefore. There's no way of JavaScript being able to handle the string 'Marge said "I'd look that was" to Peter' and you need your data be cleaned before offering it to the script?

    0 讨论(0)
  • 2020-11-28 04:26

    Use hidden spans, one each for each of the parameters <%itemid%> and <%itemname%> and write their values inside them.

    For example, the span for <%itemid%> would look like <span id='itemid' style='display:none'><%itemid%></span> and in the javascript function SelectSurveyItem to pick the arguments from these spans' innerHTML.

    0 讨论(0)
  • 2020-11-28 04:30

    Try avoid using string-literals in your HTML and use JavaScript to bind JavaScript events.

    Also, avoid 'href=#' unless you really know what you're doing. It breaks so much usability for compulsive middleclickers (tab opener).

    <a id="tehbutton" href="somewhereToGoWithoutWorkingJavascript.com">Select</a>
    

    My JavaScript library of choice just happens to be jQuery:

    <script type="text/javascript">//<!-- <![CDATA[
    jQuery(function($){
       $("#tehbutton").click(function(){
            SelectSurveyItem('<%itemid%>', '<%itemname%>');
            return false;
       });
    });
    //]]>--></script>
    

    If you happen to be rendering a list of links like that, you may want to do this:

    <a id="link_1" href="foo">Bar</a>
    <a id="link_2" href="foo2">Baz</a>
    
    <script type="text/javascript">
       jQuery(function($){
            var l = [[1,'Bar'],[2,'Baz']];
            $(l).each(function(k,v){
               $("#link_" + v[0] ).click(function(){
                    SelectSurveyItem(v[0],v[1]);
                    return false;
               });
            });
       });
     </script>
    
    0 讨论(0)
  • 2020-11-28 04:31

    If it's going into an HTML attribute, you'll need to both HTML-encode (as a minimum: > to &gt; < to &lt and " to &quot;) it, and escape single-quotes (with a backslash) so they don't interfere with your javascript quoting.

    Best way to do it is with your templating system (extending it, if necessary), but you could simply make a couple of escaping/encoding functions and wrap them both around any data that's going in there.

    And yes, it's perfectly valid (correct, even) to HTML-escape the entire contents of your HTML attributes, even if they contain javascript.

    0 讨论(0)
  • 2020-11-28 04:33

    Another interesting solution might be to do this:

    <a href="#" itemid="<%itemid%>" itemname="<%itemname%>" onclick="SelectSurveyItem(this.itemid, this.itemname); return false;">Select</a>
    

    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.

    0 讨论(0)
提交回复
热议问题