4.3. Base Class Library

The AJAX Library takes a familiar set of features from the base class library of the .NET Framework and brings it to JavaScript in the browser for you to use in building your applications. It is by no means an equivalent set of functionality as what the base class library offers, but it does go a long way toward simplifying your JavaScript coding tasks.

4.3.1. The String Class

Basic support for removing whitespace is added to JavaScript strings with new methods. The trim() method performs the equivalent of trimStart() and trimEnd(). The instance of the string is not modified by the trim calls. Instead, a copy of the string is returned with the requested change. Listing 4-14 demonstrates using the trim(), trimStart(), and trimEnd() functions.

Example 4-14. Working with the String class
<%@ Page Language="C#" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Using Trim Functions</title>

    <script type="text/javascript">

        function pageLoad(sender, args) {

            var one = ' leading whitespace';
            var two = 'trailing whitespace ';
            var three = ' leading and trailing whitespace ';

            alert('.' + one.trimStart() + '.');
            alert(one);  //the original string is not modified
            alert('.' + two.trimEnd() + '.');
            alert('.' + three.trim() + '.');

        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    </div>
    </form>
</body>
</html>

The trim methods are just shortcuts to what ...

Get Professional ASP.NET 3.5 AJAX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.