SP.Utilities.HttpUtility.ecmaScriptStringLiteralEncode(scriptLiteralToEncode) Method

Applies to: SharePoint Foundation 2010

In this article
Return Value
Applies To

Retrieves an encoded version of the specified JavaScript string.

var value = SP.Utilities.HttpUtility.ecmaScriptStringLiteralEncode(scriptLiteralToEncode);


  • scriptLiteralToEncode
    A string representation of the JavaScript string to encode.

Type: string

Return Value

Type: string

Applies To

SP.Utilities.HttpUtility Class


ECMA International develops standards for information and communication systems. For more information, visit http://www.ecma-international.org/.


The following example creates an input button on an application page that encodes the URL of the All Tasks View of the current site, and then navigates to the msn.com site.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

    var web;
    var view;
    var newUrl;
    function runCode() {
        var clientContext = new SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            web = clientContext.get_web();
            var listCollection = this.web.get_lists();
            var list = listCollection.getByTitle("Tasks");
            this.view = list.get_views().getByTitle("All Tasks");
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    function onQuerySucceeded() {

        var viewServ = this.view.get_serverRelativeUrl();
        var encodedUrl = SP.Utilities.HttpUtility.ecmaScriptStringLiteralEncode(viewServ);
        newUrl = 'http://www.msn.com';
        alert('Encoded All Tasks View URL: ' + encodedUrl);
        alert('Navigating to msn.com...');

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());


    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />


See Also


SP.Utilities.HttpUtility Methods

SP.Utilities Namespace