[转] http://ruudheemskerk.net/archive/2010/08/03/helpful-sharepoint-javascript-functions.aspx
The init.js and core.js JavaScript files in the layouts directory of SharePoint 2007 contain a lot of helpful JavaScript functions. Here is a brief list of some of the most helpful classes, functions and variables available.
If you are looking for SharePoint 2010, check out the extensive ECMAScript Class Library reference on MSDN: http://msdn.microsoft.com/en-us/library/ee538253.aspx, the SP.Utilities namespace contains loads of helpful utility functions.
This function can be used to encode javascript variable for usage in scripts.
Location
init.js
Parameters
str
The string that has to be encoded.
Return value
The encoded string that can be used in scripts.
Examplevar strAction = "STSNavigate('" + STSScriptEncode(currentItemUrl) + "')";
This function can be used to encode and quote javascript variable for usage in scripts.
Location
init.js
Parameters
str
The string that has to be encoded.
Return value
The encoded string that can be used in scripts, that is enclosed in quotes (").
Examplevar strAction = 'STSNavigate(' + STSScriptEncodeWithQuote(currentItemUrl) + ')';
This function can be used to encode a string to html.
Location
init.js
Parameters
str
The string that has to be encoded.
Return value
The string encoded to html.
Examplevar html = STSHtmlEncode('htmlString');
This function can be used to navigate to a given url.
Location
init.js
Parameters
url
A string that contains the url to navigate to.
ExampleSTSNavigate('http://www.microsoft.com');
This function can be used to validate an url whether it starts with "http" or with a slash '/'. The function raises an alert when the url is not valid.
Location
init.js
Parameters
url
A string that contains the url to check
Return value
If the url is valid it will return the url that has been given as the parameter, if it is not valid it wil return an empty string.
Examplevar url = PageUrlValidation('http://www.microsoft.com');
This function can be used to get a querystring parameter.
Location
init.js
Parameters
keyName
A string that contains the name of the parameter.
bNoDecode
A boolean that states whether the value has will not be encoded, this parameter is optional, default is false.
url
A string that contains the url to fetch the querystring parameters from, this is optional, the window.location.href will be used if the parameter is null.
Return value
The value of the parameter, encoded if bNoDecode was false.
Examplevar action = GetUrlKeyValue('action');
This function can be used to get the source parameter from the querystring.
Location
init.js
Parameters
defaultSource
A string that contains the default source, this parameter is optional.
Return value
If the source querystring parameter exists it will return the value of that parameter, otherwise if the defaultSource parameter was given it will return that. In any oter case it will return window.location.href.
Examplevar source = GetSource();
This function can be to change the current user.
Location
init.js
Parameters
url
A string that contains url to go to after the login.
bUseSource
A boolean that indicates that the source will be added to the url, otherwise the source will be the window.location.href. This parameter is optional, default is false.
Example<a href="#" onclick="javascript:LoginAsAnother('\u002f_layouts\u002fAccessDenied.aspx?loginasanotheruser=true', 0)">Log on as a different user</a>
This function can be used to navigate to an url, adding a source querystring parameter.
Location
init.js
Parameters
url
A string that contains url to navigate to.
Example<a href="#" onclick="javascript:GoToPage('/_layouts/settings.aspx')">Settings</a>
This function can be to trim spaces on a string.
Location
init.js
Parameters
str
A string that will be trimmed.
Return value
The trimmed string.
Examplevar trimmed = TrimSpaces(" string with spaces "); // Returns "string with spaces".
This function can be to trim whitespaces on a string, that is spaces, tabs and linebreaks (\t \n \r \f).
Location
init.js
Parameters
str
A string that will be trimmed.
Return value
The trimmed string.
Examplevar trimmed = TrimWhiteSpaces("\t\tstring with spaces\n"); // Returns "string with spaces".
This function takes a string and returns a URL-encoded string.
Location
init.js
Parameters
str
A string that will be encoded.
Return value
The encoded string.
Examplevar urlEncodedValue = escapeProperly("My Value"); // Returns "My%20Value".
This function takes a URL-encoded string and returns an string.
Location
init.js
Parameters
str
A string that will be decoded.
Return value
The string.
Examplevar urlDecodedValue = unescapeProperly("My%20Value"); // Returns "My Value".
The JSRequest object provides parsing of the querystring, you can easily use this object to get querystring variables, filenames and pathnames.
Location
init.js
ExampleJSRequest.EnsureSetup();
// The current url in this example is http://localhost/pages/default.aspx?debug=true
var debug = JSRequest.QueryString["debug"]; // Returns 'true'.var fileName = JSRequest.FileName; // Returns 'default.aspx'.
var pathName = JSRequest.PathName; // Returns '/pages/default.aspx'.
This array allows you to register additional JavaScript methods that should run in the body onload event.
Location
core.js
Example_spBodyOnLoadFunctionNames.push('functionName');
You can also pass arguments, for example an id. The next example shows an argument of type string._spBodyOnLoadFunctionNames.push('functionName("functionArgument")');
This variable contains the base URL of the current site or subsite.
Location
Inline
Exampledocument.location = L_Menu_BaseUrl + 'Lists/Tasks/AllItems.aspx';
This variable contains the LCID setting of the current site.
Location
Inline
This variable contains the theme name of the current site.
Location
Inline
This variable contains the id of the current user.
Location
Inline
Be careful using the out of the box scripts from the core.js when it is loaded deferred. In that case, add your scripts to the JQuery on document.ready or via _spBodyOnLoadFunctionNames to be sure that the functions are present.