in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

The Dean's Office

SuperGeek Tip: Open off-site links in a new window

I know I said I'd do these every week, but I might not be able to post this one next week, and I just can't sit on it any longer.

Many people want to modify their links list (really, just the links web part on the default.aspx of a site) so that links open in a new window.  Typically, what they REALLY want is only off-site links opening in a new window.  Up until now, you've had to modify the site definition, or (heh) tell users to hold the SHIFT key down.

Why not throw a Content Editor Web Part on the page, and use this code sample?  It will modify ALL links that leave the current domain (except JavaScript links, don't want to break the “Help” at the top of a SharePoint page) to open in a new window and, if you choose, will show a warning first.  Here's the code:


<script>
   var theLinks = document.links;
   var thisDomain = window.location.hostname;
  
   // Edit "theMessage" to your liking
   var theMessage = "You are now leaving " + thisDomain + ".  The content of the requested web site is outside our control.  The requested web page will open in a new window.  You may click \"Cancel\" to stop loading the requested page.  You may need to hold your CTRL key down when pressing \"OK\" to bypass your pop-up blocking software.";
  
   // Change "showMessage" to 0 if you don't want the warning to pop up when navigating to external sites.
   var showMessage = 1;
  
   function updateLinks() {
  for (i=0; i < theLinks.length; i++) {
   var thisLink = theLinksIdea;
   if (thisLink.href.indexOf(thisDomain) == -1 && thisLink.href.indexOf("javascript") == -1) {
    thisLink.target = "_blank";
    if (showMessage == 1) {
     thisLink.onclick = confirmNav;
    }
   }
  }
 }
 
 function confirmNav() {
  return confirm(theMessage);
 }
 
 document.body.onload = updateLinks;
</script>

Comments

 

Zeo said:

Great post !

just a mistake : var thisLink = theLinksIdea;

otherwise it does not work.

June 21, 2007 7:32 AM
 

BA said:

Actually, the line within the loop should read:

var thisLink = theLinks(i)

September 29, 2007 7:07 AM
 

Greg said:

This is good, and have been using. BUT, it breaks the MOSS sub-workspaces drop-down navigation links.

January 16, 2008 10:16 AM
 

rüya tabiri said:

Thank You.

August 29, 2008 9:31 AM
 

Bekah said:

Thank you! That was a huge help!

September 26, 2008 5:19 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts