10/02/2010

SharePoint: JavaScript in Quick Launch and Top Link Bar!

 

Update: SharePoint 2010 users can use these same techniques to display a page in a SharePoint dialog box. See here: http://techtrainingnotes.blogspot.com/2010/12/sharepoint-opening-2010-dialog-box-from.html

 

Pretty scary! After five years of working with SharePoint 2007 I’m still finding new tricks! (Yes, 12,000 people probably have already found this… but I just did!)

 

Did you know….

                                      you can put JavaScript in Quick Launch and Top Link Bar links?

 

Should have been obvious (hit’s self in head).  Where you can have “http://” you can generally have “JavaScript:”.

Example:

            <A HREF=”http://www.maxtrain.com”>  Click here </A>

            <A HREF=”JavaScript:alert(‘hi there!’)”>  Click here </A>

 

Before you go on… this works in SharePoint 2007 and SharePoint 2010, in both Quick Launch and Top Link Bar, but only in non-publishing based sites. (see the end of this article)

 

 

JavaScript in Quick Launch!

For a quick test… go to Site Actions, Site Settings, Quick Launch and add this bit of JavaScript in the URL box:

JavaScript:alert('Hello world!')

(Note: Although “JavaScript” can also be “javascript”, most everything in JavaScript must be in the correct case. You must type “alert” in all lower case.)

image

 

Now test it:

 

image

 

Now what can you do with this?

 

My head overflows with ideas!  Here’s three to start with:

  • Prompt a user before going to a page or site (“Are you sure you …”)
  • Open a link from Quick Launch or the Tabs in a new window
  • Change page languages (in SP 2010)    see here

 

Prompt user before going to a link:

 

Here’s the normal link found in Quick Launch for the Calendar: 
         “/sites/training/secure/Lists/Calendar/calendar.aspx”

image

 

Here’s a link that will prompt to see if you really want to go to the calendar: (all one line!)

JavaScript:if (confirm('do you really want to see the calendar?'))
document.location='/sites/training/secure/Lists/Calendar/calendar.aspx';

image

 

image

 

 

Here’s a more practical link that will prompt to see if you really want to go to an external site: (all one line!)

JavaScript: if ( confirm('This web site is external to the 
company and may not be safe!'
) )
document.location='http://www.google.com';

image

 

Open a link from Quick Launch or the Tabs in a new window

 

javascript: void(window.open('http://www.google.com'));

This example is in SharePoint 2010:

image

 

 

Where does it not work?  Publishing sites!

 

While this trick works from Team Sites and other non-publishing sites, it does not work form an site with the Publishing features enabled. (How do you know? Go to Site Actions, Site Settings, and see if you have “Quick Launch” or “Navigation”.  “Navigation” is for publishing sites.

The problem is the editor popup. It’s checking for things like “http://” and “mailto:” and not accepting “JavaScript”.

 

image

 

 

.

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.