Some useful JavaScript methods Variable and objects in Sharepoint 2013

In my previous blog I explored _spPageContextInfo variable properties .This time I explored SharePoint provides some useful methods and objects in JavaScript. Followings are some methods and objects which I use frequently in my many articles. Here is a list of some useful methods and objects in JavaScript that I collected. All these are available Out-Of-The-Box, you don’t need to add any JavaScript libraries.

– JSRequest (Object):
Getting Query String Values using The JSRequest class is a JavaScript object in SharePoint
Before using any of these properties, you should call JSRequest.EnsureSetup();

Ex: page url is http://Siteur/ListName/EditForm.aspx?ID=9
To get a querystring value

var Id = JSRequest.QueryString[“ID”]; // Id= 8

Similarly, you can use
var fileName = JSRequest.FileName; // current page name
var pathname = JSRequest.PathName; // server relative url

– GetUrlKeyValue(parameter, noDecode, url) (Method):

This Sharepoint utilities and functions available on the client side SharePoint 2013 to get the value from the querystring of the url. GetUrlKeyValue will help us in getting the value from the query string
alert(GetUrlKeyValue(‘a’, false, ‘’));

The above statement will return the value ‘first’. Here we are specifying our own url.
alert(GetUrlKeyValue(‘S’, false));
The above statement will look for a query string variable ‘S’ in the browser url, and returns the decoded value.

The above statement will look for a query string variable ‘S’ in the browser url.

– SetUrlKeyValue (parameterName, parameterValue, bEncode, url):

This Sharepoint utilities is opposite to GetUrlKeyValue. In GetUrlKeyValue function, we will getting the value from the querystring and in SetUrlKeyValue, we will be set the value to querystring .

parameterName: The querystring name to which we need to set the value

parameterValue: The parameter value that needs to be set to the keyName

bEncode: Whether the value needs to be encoded in the url before setting the value

url: Url to which the value needs to be set

SetUrlKeyValue(‘EditPage’, ‘true’, false, window.location.href);
SetUrlKeyValue(‘EditPage’, ‘true’, false,’);

– SP.ScriptUtility : This class is the home to six very useful helper methods and one helper field.

– emptyString : This field returned an empty string in JavaScript “ ”
SP.ScriptUtility Methods
– isNullOrEmptyString – Checks if the specified text is null, an empty string or undefined
– isNullOrUndefined – Checks if the specified object is null or undefined.
– isUndefined – Checks if the specified object is undefined.
– truncateToInt – Returns largest integer value that is less than or equal to the specified number if the number is greater than 0 else returns the smallest integer value that is greater than or equal to the number.
– ScriptUtility : It merely acts as a placeholder for the class name SP.ScriptUtility to hold the Above methods.

– escapeProperly(str) (Method)

This function returns escapeProperly function returns the encoded value for provided string.
var s = escapeProperly(“hello world!!”); //s = “hello%20world%21%21″

– SP.Guid :

The function SP.Guid.newGuid().toString() returns new GUID at runtime.

var guid=SP.Guid.newGuid().toString();//guid =”260868ac-2c0c-4494-a40e-22b00aaa0312”

– unescapeProperly(str) (Method):
This function returns unescapeProperly function returns decoded string for provided encoded string.

var Outstr=unescapeProperly(“this%20is%20a%20test”)//Outstr=”this is a test”

– escapeProperly(str) (Method):
This function returns escapeProperly function returns the encoded value for provided string.
var urlEncodedValue = escapeProperly(“My Value”); // Returns “My%20Value”.

– TrimSpaces(str)
This function can be to trim spaces on a string. A string that will be trimmed. Return value he trimmed string.

var trimmed = TrimSpaces(” string with spaces “); // Returns “string with spaces”.

– TrimWhiteSpaces(str)
This method trims the white spaces of a string and It also removes spaces created spaces, tabs and linebreaks (\t \n \r \f). A string that will be trimmed.Return valueThe trimmed string.

var trimmed = TrimWhiteSpaces(“\t\tstring with spaces\n”); // Returns “string with spaces”.

– variable :L_Menu_BaseUrl
This variable contains the base URL of the current site or subsite.
Ex. document.location = L_Menu_BaseUrl + ‘Lists/Tasks/AllItems.aspx’;

– variable: L_Menu_LCID
This variable contains the LCID setting of the current site.

– Variable: L_Menu_SiteTheme
This variable contains the theme name of the current site.

Lots of this JavaScript Function is already written for you in SharePoint’s “Init.js“and “core.js” file follow the below tables:

Init.js (Function)
– ULSTrim(str)
– ULSEncodeXML(str)
– PageUrlValidation(url)j GetCurrentEltStyle(element, cssStyle)
– IsCheckBoxListSelected(checkboxlist)
– STSHtmlEncode(str)
– DeleteCookie(sName)
– GetCookie(sName)
– navigateMailToLink(strBody)
– navigateMailToLinkWithMessage(strTo, strBody)
– makeAbsUrl(strUrl)
– HideMenuControl(menuControlId)
– displayPNGImage(id,src,width,height,alt)
– GetUrlKeyValue(keyName, bNoDecode, url)
– GoToPage(url)
– TrimSpaces( str )
– TrimWhiteSpaces( str )
– FormatDate(sDate, sTime, eDate, eTime)
– GetElementByClassName(elem, classname)
– WpClick(evt)
– GetViewportHeight()
– GetViewportWidth()
– RemoveQueryParameterFromUrl(stURL, stParameterName)
– HasValidUrlPrefix(url)
– AbsLeft(obj)
– AbsTop(obj)
– ExecuteOrDelayUntilScriptLoaded(func, depScriptFileName)
– ShowPopupDialog(dlgUrl)

RefreshPageTo(evt, url, bForceSubmit)
OpenPopUpPage(url, callback, width, height)
RemoveUrlKeyValue(keyName, url)
ShowInPopUI(evt, currentCtx, strUrl)
OpenPopUpPageWithTitle(url, callback, width, height,title)
_addNotificationInternal(span, strHtml, bSticky, tooltip, onclickHandler, bNoAnimate)
removeNotification(id, bNoAnimate)
SetCookie(name, value, path)

Hope this will be useful.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.