SharePoint Events

  5/13/2013 - Conference: SharePoint Summit 2013
  5/21/2013 - Webcast: SharePoint 2013 and ECM: Content Migration and Storage
  5/22/2013 - Webcast: Managing CAD in SharePoint
  5/23/2013 - Webcast: SharePoint Document Automation and E-Forms for Financial Services
  5/24/2013 - Webcast: What's New in Search for SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  SharePoint 2013 Launch
  SharePoint 2013 Migration and Governance
  SharePoint 2013 and Enterprise Content Management
  Top Benefits of SharePoint 2013
  What's New in Business Intelligence in Office and SharePoint 2013
  SharePoint and Office 2013 Integration
  SharePoint 2013 Infrastructure Preview
  SharePoint, Lync, and, Exchange in the Cloud with Office 365
  Advanced Reporting in SharePoint with Microsoft Power View

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
Adding JavaScript NameSpaces to Your MasterPage

By: Omar Stewart

 

For those of you who are unfamiliar with JavaScript namespaces, they basically serve the same purpose as a namespace in other languages. In JavaScript, it can be a neat and clean way to wrap all your JavaScript functionality. This makes for an even cleaner implementation when you load your JavaScript from an external file. In this tutorial, I'll show you how to quickly set up your custom namespace and load it into your SharePoint MasterPage.

 

I'll begin by loading Randy Drisgill's Starter Masterpage, available on codeplex.

 

Adding_JavaScript_NameSpaces_to_Your_MasterPage

 

Now I'll create a document library where my script files will live. From SharePoint Designer, I'll just create a new document library and call it "Script Library"..

 

Adding_JavaScript_NameSpaces_to_Your_MasterPage

 

In my script library, I'll create a new javascript file and call it "myJavaScriptFile.js"

 

Adding_JavaScript_NameSpaces_to_Your_MasterPage

 

We'll create our namespace within a function just to make it easy for SharePoint to access.

 

//Create our wrapper function

function TheSharePointBlogScript() {

 

//Check if our namespace exists

if (!TheSharePointBlog)

 

//If not, Declare our namespace

TheSharePointBlog  =  {

 

//Define functions

grabAllLinks : function() { document.getElementsByTagName ("A"); }

 

grabAllTableRows : function() { document.getElementsByTagName("TR"); }

 

printError : function(err) { alert("err") }

};

}

}

 

I'll save my changes.

 

Now that I have my file, I can drop it into my masterpage. The good folks in Redmond have made It very simple to call a function after when our masterpage loads.

 

The  _spBodyOnLoadFunctionNames array is an array of functions that gets loaded when a SharePoint page is loaded. All we need to do is add our wrapper function to that array.

 

Lets jump back to our starter master page, scroll down  to the HEAD tag and add our external script file.

 

<script language="javascript" type="text/javascript" src="../ScriptLibrary/myJavaScriptFile.js"></script>

 

Now I can actually add our function to the Array..

 

Adding_JavaScript_NameSpaces_to_Your_MasterPage

 

<script type="text/javascript" >

_spBodyOnLoadFunctionNames("TheSharePointBlogScript");

</script>

 

Our function will now be created masterpage loads and we can access our namespace object throughout the document..

 

<script type="text/javascript">

Var rows = TheSharePointBlog .grabAllTableRows ()

</script>

 

Adding_JavaScript_NameSpaces_to_Your_MasterPage 

 

This is a nice way to create manageable javascript in SharePoint.

 

By: Omar Stewart

        

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy