SharePoint + jQuery = Stay Here Feature
Fun with jQuery and SharePoint
In my previous post I showd how to create a slideshow in SharePoint with the help of jQuery and the Cycle plugin. Yesterday someone asked me for a feature that takes care of opening all external urls in a new window. That makes sense doesn't it. You do not want visitors to leave your SharePoint site to soon. You want them to Stay Here. And with our jQuery feature in place it's a piece of cake so I decided to create the motion10 Stay Here feature.
return !this.target && this.hostname && this.hostname !== location.hostname;
How to create a feature
Actually we would like to have this script in all pages of our sitecollection. Hmmmm.... that sounds familiar doesn't it? Again we can use the Delegate Control to add this script to the head of every page. So the script goes to the layouts directory and we create a User Control that just contains a script reference.
<%@ Control="" Language="C#" ClassName="jQueryStayHereControl" %>
And just like we did with our Cycle plugin we add this control to our elements collection.
<?xml version="1.0" encoding="utf-8" ?>
We must not forget our jQuery dependency though. So in our feature.xml we add an Activation Dependency and that's all.
<?xml version="1.0" encoding="utf-8"?>
Title="motion10 jQuery Stay Here Plugin"
Description="If you enable this feature all external links on your site will be opened in a new window."
By activating the feature the user control is added to the head of our page which loads the script which in turn makes all of our links to external websites open in a new window.
Please do feel free to download the motion10 SharePoint Solution Pack. It contains all of the source and an installation folder. Oh... and you might find some extra features in there. I'll try to add a new feature every two weeks and update the package. So if you do have some good ideas for features just leave me a comment or drop me an email and we'll se what I can do. I hope my next feature will enable Google Analytics on your site. Busy with that one because I need to add an application page to enable you to configure your Google user account id.