Passing a parameter to function with single quote

前端 未结 5 1065
抹茶落季
抹茶落季 2021-01-07 19:08

How do I pass a parameter to a javascript function with \' included

var name =\"Lauren O\'Donald\";

var htmlAnch=\'

        
相关标签:
5条回答
  • 2021-01-07 19:53

    You can escape it using \:

    var htmlAnch='<a onclick="javascript:selectEmployee(1100,\'Lauren O\'Donald\');return false;" 
                 href="javascript:void(0);">O\'Donald, Lauren</a>';
    

    However as you've tagged this question with jQuery,a better solution is to hook up an event to the element and use data-* attributes to store the relevant information, which will avoid the use of ugly onX attributes. Try this:

    var $htmlAnch = $('<a />' {
        text: "O'Donald, Lauren" ,
        data-id: 1100,
        data-name: "Lauren O'Donald"
    }).click(function(e) {
        e.preventDefault();
        selectEmployee($(this).data('id'), $(this).data('name'));
    });
    
    $(document).append($htmlAnch);
    
    0 讨论(0)
  • 2021-01-07 19:55

    Write your own function to return a escaped string. Demo

    Pass your string as argument to this function and you will get escaped string. You can also add more characters to blocklist if you want to escape some more characters

    function remove_quotes(values1)
    {
            var values = values1.toString();
            var str = "";
            var blockList = ['"','\'','\\']; // This is the list of key words to be escaped
            var flag = 0;
            for(var i = 0;i<values.length;i++)
            {
                for(var j=0;j<blockList.length;j++)
                {
                    if(values[i] == blockList[j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0)
                str += values[i];
                else
                {
                    str += '\\';
                    str += values[i];
                    flag = 0;
                }
            }
            return str;
    
        }
    
    0 讨论(0)
  • 2021-01-07 20:07

    try something like this

        var htmlAnch='<a onclick="javascript:selectEmployee(1100,\'Lauren O\'Donald\');return false;" href="javascript:void(0);">O\'Donald, Lauren</a>';
    
    0 讨论(0)
  • 2021-01-07 20:12

    You can escape quotes/characters by prepending \ to it:

    var string = 'my string with "double quotes" and \'single quotes\'';
    var string = "my string with 'single quotes' and \"double quotes\"";
    //                                               ^              ^
    

    Using a dynamic string:

    var foo = "bar with 'quotes'";
    var string = 'my string with "double quotes" and ' + foo.replace(/'/g, "\\'");
    //my string with "double quotes" and bar with \'quotes\'
    
    0 讨论(0)
  • 2021-01-07 20:12

    In .aspx file you can do like as below:

    <a data-open="editTemplateOverrideModal" onClick="populateUp
    dateModal('<%#Server.HtmlEncode(Convert.ToString(DataBinder.Eval(Container.DataItem, "Description")).**Replace("'", "\'")) %>')**">
    
    0 讨论(0)
提交回复
热议问题