Some useful Javascript variables/functions in Sharepoint

Here is a list of some useful Javascript variables/functions that I collected from many articles online. I have tested all these variables/functions in SharePoint 2010. All these are available Out-Of-The-Box, you don’t need to add any javascript libraries.

1. _spUserId (Variable)

This variable gives the ID of the logged in user. For an anonymous user, this variable will be empty.

var uid = _spUserId;

You can test this variable in the address bar of your browser. Try

javascript:alert(_spUserId);

You will see an alert message with the ID of the logged in user.

2. JSRequest (Object)

Using this JSRequest object, we can get the querystring, pathname and filename. Before using any of these properties, you should call JSRequest.EnsureSetup();

Ex: page url is http://www.xyz.com?qid=15

To get a querystring value

JSRequest.EnsureSetup();
var q = JSRequest.QueryString["qid"]; // q = 15

Similarly, you can use

JSRequest.EnsureSetup();
var f = JSRequest.FileName; // current page name
var p = JSRequest.PathName; // server relative url

3. GetUrlKeyValue(parameter, noDecode, url) (Method)

GetUrlKeyValue() is a javascript function using which we can get the Query string parameter either from url in the browser or a url that we specify.

parameter(string): query string parameter from the url.

noDecode(bool): specifies whether the value has to be encoded or not. If false value is decoded, else returned as it is.(Optional)

url(string): the url from which Query string values are to be retrieved.(Optional)

Ex:

alert(GetUrlKeyValue(‘a‘, false, ‘www.xyz.com?a=te%20st‘));

The above statement will return the value ‘te st’. Here we are specifying our own url.

alert(GetUrlKeyValue(‘a‘, false));

The above statement will look for a query string variable ‘a’ in the browser url, and returns the decoded value.

alert(GetUrlKeyValue(‘a‘));

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

4. _spPageContextInfo (Object)

_spPageContextInfo object has several useful properties, some are

a. webServerRelativeUrl (for current web)
b. siteServerRelativeUrl (current site collection url)
c. webLanguage (for localization)
d. currentLanguage (for localization again)
e. webUIVersion
f. userId (current user id just like _spUserId)
g. alertsEnabled (more for current page if it has any alerts on it)
h. allowSilverlightPrompt (to have that prompt or not on the page)
i. pageItemId
j. pageListId (Guid)

We can get the webServerRelativeUrl simply by saying

var url = _spPageContextInfo.webServerRelativeUrl;

All the remaining object properties can be used in the same way.

5. escapeProperly(str) (Method)

This function returns the URL encoded value of a given string.

var s = escapeProperly("hello world!!"); //s = "hello%20world%21%21"

6. unescapeProperly(str) (Method)

This function decodes a URL encoded string.

var s = unescapeProperly("hello%20world%21%21"); //s = "hello world!!"

7. STSHtmlEncode(htmlString) (Method)

This function encodes an html string

var s = STSHtmlEncode("<p>sample text</p>");
//s = "&lt;p&gt;sample text&lt;/p&gt;"

8. TrimSpaces(str) (Method)

This method trims out the leading and trailing white spaces of a string. It doesn’t remove spaces created by special characters such as ‘\n’, \t’

var s = TrimSpaces("   Hello   World!!    "); //s = "Hello   World!!"

I intentionally put more spaces between ‘Hello’ and ‘World!!’ just to show that this method doesn’t remove any spaces between words.

9. TrimWhiteSpaces(str) (Method)

This method trims out the leading and trailing white spaces of a string. It also removes spaces created by special characters such as ‘\n’, \t’

var s = TrimWhiteSpaces("\n\nHello   World!!\t\t"); //s = "Hello   World!!"

10. LoginAsAnother(url, bUseSource)

This method is used to login as different user.(as the name says)

url(string): the url of the page to which the new user has to be sent after login.

bUseSource(boolean): 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.

<a href="#" onclick="javascript:LoginAsAnother(‘\u002f_layouts\u002fAccessDenied.aspx?loginasanotheruser=true‘, 0)">Log on as a different user</a>

11. STSPageUrlValidation(url)

This function validates a url if it starts with “http” or “/” or “:” ONLY. It returns the url value back if it is a valid url and an empty value if it is an invalid url. If the url is not valid, an alert message is displayed that says “Invalid page URL:”.

var s = STSPageUrlValidation("praneethmoka.wordpress.com"); //s = praneethmoka.wordpress.com

var s = STSPageUrlValidation(":praneethmoka.wordpress.com"); //s = "";

var s = STSPageUrlValidation("w.wordpress.com"); //s = "w.wordpress.com";

Now please don’t ask me what kind of a validation this is.

STSPageUrlValidation(url) in turn calls a method PageUrlValidation(url) and here’s the code for PageUrlValidation method

function PageUrlValidation(url)
{  ULSA13:;
   if((url.substr(0, 4) == "http") || (url.substr(0, 1) == "/") || (url.indexOf(":") == -1))
   {
      return url;
   }
   else
   {
      var L_InvalidPageUrl_Text="Invalid page URL: ";
      alert(L_InvalidPageUrl_Text);
      return "";
   }
}

I will update this post as I find more useful javascript stuff.

原文地址:https://praneethmoka.wordpress.com/2012/01/12/some-useful-javascript-variablesfunctions-in-sharepoint/

原文地址:https://www.cnblogs.com/bjdc/p/10943438.html

时间: 2024-11-10 13:18:00

Some useful Javascript variables/functions in Sharepoint的相关文章

Understand JavaScript Callback Functions and Use Them

In JavaScript, functions are first-class objects; that is, functions are of the type Object and they can be used in a first-class manner like any other object (String, Array, Number, etc.) since they are in fact objects themselves. They can be “store

[Javascript] Promise-based functions should not throw exceptions

Source You can also start a chain of then() method calls via Promise.resolve() and execute the synchronous code inside a callback: function asyncFunc() { return Promise.resolve() .then(() => { doSomethingSync(); return doSomethingAsync(); }) .then(re

jquery.i18n.js

/****************************************************************************** * jquery.i18n.properties * * Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and * MIT (http://dev.jquery.com/browser/trunk/jquer

关于在SharePoint 2013(2010)中Javascript如何实现批量批准的自定义操作功能?

1.概述: SharePoint 2013(包括SharePoint 2010)提供了很方便的,多选的界面,但是很多操作还是不能批量进行,比如:批准的功能.如果您要解决方案不关心代码,那么请直接联系作者.如果您对技术感兴趣,那么下面的组合拳就是告诉你如何在2013的Ribbon的工具栏上实现这个小功能,整个实验必须要有SPD(SharePoint Designer 2013),要使用到Javascript的很多知识.作者完全从实际出发,应对了在这个过程中可能出现的各种各样的"状况",比

在SharePoint解决方案中使用JavaScript

在SharePoint解决方案中使用JavaScript (0) 随着Web前段技术(JavaScript/HTML5)的日益发扬光大,在Web应用程序中,我们开始更多的使用JavaScript.很多以往是放在服务器上运行的逻辑,现在都开始逐渐的向前段转移.这种趋势不需要作者多说,只要是Web开发人员(包括SharePoint工程师),都会有所体验.而在SharePoint平台,这种前端化的趋势也是相当明显的.当我们构建SharePoint解决方案的时候,JavaScript代码的数量在不断的增

在SharePoint解决方案中使用JavaScript (1) – 引用.js文件

本文是系列文章的第一篇. 在SharePoint解决方案中使用JavaScript (0) ? 作为在SharePoint应用程序中使用JavaScript的第一步,就是要知道如何将一个写好的.js文件,引用到页面上.嗯,你可能觉得这个话题太简单了,"引用一个.js文件不就是在页面上方加一个<script>标签吗?"但是我们要考虑的事情,可通常要比这复杂得多.比如,我们大部分的.js文件,可能都是需要放置在网站中的所有页面上的,修改网站里面的每一个.aspx显然不是好主意,

Javascript.ReactNative-2-javascript-syntax-in-react-native

JavaScript Syntax in React Native Contents: Arrow Function Let+Const Default + Rest + Spread Destructuring assignment Computed object property names Classes for ... of Template Strings Reference 1. Arrow Function 1.1 Arrow Function的特征 A: Arrows are a

JavaScript的执行上下文

在JavaScript的运行过程中,经常会遇到一些"奇怪"的行为,不理解为什么JavaScript会这么工作. 这时候可能就需要了解一下JavaScript执行过程中的相关内容了. 执行上下文 在JavaScript中有三种代码运行环境: Global Code JavaScript代码开始运行的默认环境 Function Code 代码进入一个JavaScript函数 Eval Code 使用eval()执行代码 为了表示不同的运行环境,JavaScript中有一个执行上下文(Exe

Google JavaScript Style Guide

转自:http://google.github.io/styleguide/javascriptguide.xml Google JavaScript Style Guide Revision 2.93 Aaron Whyte Bob Jervis Dan Pupius Erik Arvidsson Fritz Schneider Robby Walker Each style point has a summary for which additional information is ava