SharePoint and jQuery

So I had another interesting project recently where I needed to integrate jQuery and a jQuery plugin into a SharePoint site. Here are some notes from that experience that will, hopefully, help others out.

  1. Create a custom master page and “embed” jQuery in the <head> (not linked to an external file, but actually copy and paste the jQuery minified code within a script tag). This way you won’t have to deal with path issues. <Update 4-14-2013:> You’re better off placing the jQuery library under the Style Library and calling it from within your master page’s <head>. You will need to determine the file path though, but it should be fairly easy to figure out. </end update> Apply your custom master page to your site (and any subsites you want it applied to). This master page can be a copy of the basic v4.core if you want. All we’re doing is adding the jQuery framework into a script tag in the <head>.
  2. To use a jQuery plugin, first create a custom library to hold your jQuery scripts, plugins and your HTML document with your working code.
  3. Then use the HTML Page Viewer web part to bring in your HTML document to whatever SharePoint page you need (in an iframe). And everything should work as expected.

One interesting issue I had with this project was the fact that upper and lowercase URLs went to the same page but in my script this was two different URLs. So to get around this in my script, I had to grab the window location, turn it into a string and then lowercase it, before applying other jQuery cool stuff for a specific page.

Anyway, that’s what I learned. I hope it’s useful!

Maybe I’ll turn that into a jQuery plugin…

So I had an interesting project recently where they needed a way to “slide” the pages in and out. At first I thought this would be a simple problem to solve as jQuery has so many slider plug-ins that it’s practically a cottage industry. But there was a problem. The site content had already been created and this slide effect needed to be bolted on “after the fact.” After various trials with different jQuery sliders, I thought I found a good enough solution. Unfortunately it would require the client to re-write their content into the new “model” which would be a fair amount of work.

And I thought I was done – it met the clients’ needs, they were happy with the solution even though they would need to put in a lot of work to get their existing content into it. They understood the technical reasons why they would need to do this.

But…this was such a fascinating problem that I couldn’t stop playing around and seeing if there might be a better solution. Something a little more light-weight and that would play well with the existing content.

So to better the world here is my code – I don’t claim it’s the best, I don’t claim it will solve *your* problems, and I certainly don’t claim any guarantees. Use at your risk, no support provided, yada, yada.

First our HTML (cut down to just the important parts):

 <nav>
        <ul>
            <li><a href="page1.html" data-href="#page1">Page 1</a></li>
            <li><a href="page2.html" data-href="#page2">Page 2</a></li>
            <li><a href="page3.html" data-href="#page3">Page 3</a></li>
            <li><a href="page4.html" data-href="#page4">Page 4</a></li>
        </ul>
</nav>
<div id="pager">
        <div id="page">
        Enable javascript for best results...
        </div>
</div>

So a little explanation of the above html. We are using HTML5, we have our navigation with the links, the links have the href attribute to provide accessibility in case Javascript is disabled, and we have a custom data- attribute allowed by HTML5 so we can use this as our hooks to do all the cool jQuery action. Finally we have our “pager”, a container to hold our sliding “pages”, and of course, our “page” where we will load in our content.

And the CSS:

body{
    overflow-y: scroll;
}
#pager{
    width:960px;
}
#page{
    position:relative;
    left:0px;
}

.pageTile{
    max-width:900px;
    min-width:320px;
    min-height:420px;
/*This class comes from the content pages we're loading in*/
}

Now for the jQuery fun (please note I do use the jQuery easing plugin to provide more easing options):

 <script>
        $('.pagerNav li a').attr('href', '#');
        $('#page').load('page1.html');
        jQuery.easing.def = "easeInOutQuad";
        $('a[data-href="#page1"]').click(function(){
            $('#page').animate({left:'-3000px'}, 300, function() {
                $('#page').css({left:'3000px'});
                $('#page').load('page1.html');
                window.location = window.location + '/page1.html';
            });
            $('#page').animate({left: '0px'}, 300);
        });
        //repeat as needed for all your "pages" you'll be loading in
 </script>

So let’s go through the script. First, we’ll replace the href attribute of our navigation thus ensuring that only the Javascript functionality will trigger showing the content. Again, if no Javascript is available, then the href will act as normal. Next we load in content to the page using jQuery’s .load() function so users aren’t staring at a blank page when they first visit. Next we define our custom easing we want to use (again, using the excellent jQuery Easing plugin). We then capture the “click” of the link. This triggers the “page” to slide off left. Once that’s complete, we reset the “page” position way off screen on the right where we also load in the new content. We then change the browser window’s location to help users understand where they are. And then we bring our “page” back on screen with it’s new content, animating it sliding in from the right to the left.

This is pretty basic code, it should work well and not impact other jQuery plugins or even jQuery versions. And it looks like I could fairly easily turn it into a plugin.

August 2010 updates

Just a heads up that there’s new work in my portfolio, both work and personal. Also I’ve updated the resume to highlight some of the new software and skills I’ve picked up.