Search code examples
javascriptasp.netspecial-characters

Avoiding special characters in url in javascript


i have a treeview in that adding nodes dynamically and adding javscript click function on each node.. here thisFileNode.Value have url values,avoiding spl charcters in url i have used escape function in my code.. still its not working .. can anyone help me please

i dont familar with escape, what it actually does??? and how to work on that?

   thisFileNode.NavigateUrl = "javascript:clickNode(this, escape('" + thisFileNode.Value + "'));"

if any spl characters in the url it should deleted and how can i get the valid url??


Solution

  • Or alternatively, you may manually escape special characters:

    thisFileNode.NavigateUrl = "javascript:clickNode(this, '" + HtmlEscape(thisFileNode.Value) + "');"
    

    HtmlEscape method:

    public static string HtmlEscape(string str)
        {
            return str
            .Replace("&", "&")
            .Replace("\"", """)
            .Replace("'", "'")
            .Replace("<", "&lt;")
            .Replace(">", "&gt;");
        }
    }