Thursday, December 15, 2011

Useful Sharepoint Javascript Functions


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.

function TrimSpaces(str)
This function can be to trim spaces on a string.
Location
init.js

Parameters
str
  A string that will be trimmed.
Return valueThe trimmed string.

Example
var trimmed = TrimSpaces(" string with spaces "); // Returns "string with spaces".
function TrimWhiteSpaces(str)
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 valueThe trimmed string.
Examplevar trimmed = TrimWhiteSpaces("\t\tstring with spaces\n");  // Returns "string with spaces".
function escapeProperly(str)
This function takes a string and returns a URL-encoded string.
Location
init.js

Parameters
str
   A string that will be encoded.
Return valueThe encoded string.
Examplevar urlEncodedValue = escapeProperly("My Value"); // Returns "My%20Value".
function unescapeProperly(str)
This function takes a URL-encoded string and returns an string.
Location
init.js

Parameters
str
   A string that will be decoded.
Return valueThe string.
Examplevar urlDecodedValue = unescapeProperly("My%20Value"); // Returns "My Value".
class JSRequest
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'.

function LoginAsAnother(url, bUseSource)
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>

function GetUrlKeyValue(kenName, bNoDecode, url)
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.
Example
var action = GetUrlKeyValue('action');


variable L_Menu_BaseUrl
This variable contains the base URL of the current site or subsite. 
Location
Inline

Exampledocument.location = L_Menu_BaseUrl + 'Lists/Tasks/AllItems.aspx';
variable L_Menu_LCID
This variable contains the LCID setting of the current site.
Location
Inline
variable L_Menu_SiteTheme
This variable contains the theme name of the current site.
LocationInline
variable _spUserId
This variable contains the id of the current user.
Location
Inline


No comments:

Post a Comment