Pages

Showing posts with label JavaScripts. Show all posts
Showing posts with label JavaScripts. Show all posts

Wednesday, 14 August 2013

Full URL of your SharePoint 2013 site in JavaScript


If you want to use the Full URL of your SharePoint site in JavaScript, you can simply use the below window property. "window.location.protocol + '//' + window.location.host;"
For site collection and webs Server Relative URL use "_spPageContextInfo.siteServerRelativeUrl" and "_spPageContextInfo.webServerRelativeUrl"


Read more: Learning SharePoint

Monday, 16 April 2012

Javascript Remove a parameter from the query sring if found in current url

This Javascript function removes the supplied parameter from the query string if it is present in the current url. This is done by converting the query string into an array of parameters and values, then rebuilding the final string to exclude the parameter supplied to the function. The browser is then redirected to the current page with the new query string.
       
function removeParameter(parameter)
{
   //Get Query String from url
   fullQString = window.location.search.substring(1);
   
   paramCount = 0;
   queryStringComplete = "?";

   if(fullQString.length > 0)
   {
       //Split Query String into separate parameters
       paramArray = fullQString.split("&");
       
       //Loop through params, check if parameter exists.  
       for (i=0;i<paramArray.length;i++)
       {
         currentParameter = paramArray[i].split("=");
         if(currentParameter[0] == parameter) //Parameter already exists in current url
         {
            //don't include existing (will be appended to end of url)
         }
         else //Existing unrelated parameter
         {
            if(paramCount > 0)
               queryStringComplete = queryStringComplete + "&";
           
            queryStringComplete = queryStringComplete + paramArray[i];
            paramCount++;
         }
       }
   }
   
   window.location = self.location.protocol + '//' + self.location.host + self.location.pathname + queryStringComplete;
}

       
 

Javascript Get the value of the specified Query String Parameter

This Javascript function checks the address of the current page for the supplied query string parameter. If found, the value of the parameter is used, if the parameter is not found, false is returned. See also: checkParemeterExists(), which returns true or false if the specified parameter is found or not.
       
function getParameter(parameter)
{
   fullQString = window.location.search.substring(1);
   paramArray = fullQString.split("&");
   
   found = false;
   for (i=0;i<paramArray.length;i++)
   {
     currentParameter = paramArray[i].split("=");
     if(currentParameter[0] == parameter)
        return currentParameter[1];
   }
   return false; //Not found
}
       
 

Javascript Check if Query String Parameter Exists in current URL

This Javascript function checks if the parameter supplied exists in the query string. If the parameter supplied is found in the query string of the current URL, the function will return true. If the parameter is not found, false is returned. See also: getParameter() - This function returns the value of the parameter if found, and false if it is not found which can be used instead of the checkParameterExists() function below
       
function checkParemeterExists(parameter)
{
   //Get Query String from url
   fullQString = window.location.search.substring(1);
   
   paramCount = 0;
   queryStringComplete = "?";

   if(fullQString.length > 0)
   {
       //Split Query String into separate parameters
       paramArray = fullQString.split("&");
       
       //Loop through params, check if parameter exists.  
       for (i=0;i<paramArray.length;i++)
       {
         currentParameter = paramArray[i].split("=");
         if(currentParameter[0] == parameter) //Parameter already exists in current url
         {
            return true;
         }
       }
   }
   
   return false;
}

       
 

Friday, 13 April 2012

Customize sharepoint notification bar


Sharepoint 2010 site provides an area called notification bar. This bar appears when you open Edit control box. Using sharepoint client side API, you can use it. Just paste the below code in content editor web part.


<script type="text/javascript">
var notifyid;
function CreateNotification() {
notifyid = SP.UI.Notify.addNotification(" My HTML Notification ", true, "My Tooltip", "HelloWorld");
alert("Notification id: " + notifyid);
}
function RemoveNotification() {
SP.UI.Notify.removeNotification(notifyid);
}
</script><input onclick="CreateNotification()" type="button" value="Create Notification"/> <br/><input onclick="RemoveNotification()" type="button" value="Remove Notification"/>

Tuesday, 27 March 2012

The Best Way to Add Custom JavaScript and jQuery to SharePoint

During extensive SharePoint user interface customization you'll likely encounter a scenario where you need to make a web part or user control do something it was not intended to do or have a look that cannot be accomplished using the CSS hooks provided out-of-the-box. The solution is to create a custom master page and include a reference to a JavaScript file where you can modify the Document object. While straight JavaScript will do, I prefer to use the jQuery JavaScript library, which is far more robust, easier to use, and allows for plugins. Follow the steps below to add jQuery to your master page.
       
Go to jquery.com and download the latest jQuery library to your desktop. You want to get the compressed production version, not the development version.
Open SharePoint Designer (SPD) and connect to the root level of your site's site collection.
In SPD, open the "Style Library" folder.
Create a folder named "Scripts" inside of the Style Library.
Drag the jQuery library JavaScript file from your desktop into the Scripts folder.
In the Scripts folder, create a new JavaScript file and name it (e.g. "actions.js").
Open your master page file in SPD.
Within the <head> tag of the master page, add a script reference to the jQuery library just above the content place holder named "PlaceHolderAdditonalPageHead" (and above your custom CSS references, if applicable) as follows:
<script src="/Style%20Library/Scripts/{jquery library file}.js" type="text/javascript"></script>
Immediately after the jQuery library reference add a script reference to your custom scripts file as follows:
<script src="/Style%20Library/Scripts/actions.js" type="text/javascript"></script>
       
 
Your custom master page now includes jQuery and a reference to your custom scripts file where you can add jQuery scripts. SharePoint includes a number of JavaScript files throughout the site, so be careful that the scripts you add do not conflict with SharePoint's; the jQuery library itself does not conflict with SharePoint.

Popular Posts