Monday, February 21, 2011

SharePoint Link list: Open in a New Window

In SharePoint, We can put any URLs in the Links list. Unfortunately, when clicking these links, the default behavior is for the page to open in the current browser window. That is, it does NOT open the page in a new instance of the browser. Often this is annoying, isn't it?

How to open SharePoint link list item in a new window? Well, There are many ways to solve this issue:

1. Simplest solution is, Enable publishing feature, Create a site column of "Hyperlink with formatting and constraints for publishing" type, Add this Site column in your new list. Now you will get an option to open in new window! Here is how:

Step 1: Create a Site column of type "Hyperlink with formatting and constraints for publishing"
(Site Actions >> Site settings >> Site columns >> Create )

Step 2: Add this Site column in your new list
(List Settings >>Add from Existing)

Now, You will get a option to specify whether it should open in New window.

sharepoint link list open in a new window

This can be used in SharePoint 2010 links list open link in new window and in SharePoint 2007 also.

2. You can use SPD to convert the link list to XSLT Data view and get in to the code, find the tag <A href> and add an attribute target="_blank".

3. You can edit the Schema file for the link list items to open in new window. Here is how:
  • Open the schema file for the links list feature.  usuall, it located at: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\LinksList\Links\schema.xml
In this file there should be two instances of the following:
 < Column Name ="URL" HTMLEncode ="TRUE" />< HTML ><! [CDATA[" > ]] ></ HTML >

Replace them both with this:
< Column Name ="URL" HTMLEncode ="TRUE" />< HTML ><! [CDATA[" target="_blank" > ]] ></ HTML >

  • Then do the IISReset, and now all the links in your links lists will open a new browser window!
4.  You can use jQuery to modify all links attribute, so that all links in SharePoint site, will open in new window.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript">
$('a[href$="/"]').attr("target", "_blank");
</script>

5. You can Make Selected Links in a Links List Open in a New Window with Little JavaScript and content editor web part. Here is how:
  • when creating links in the Links List, add #openinnewwindow on the end of each link that you would like to open in a new window.
  • Add the content editor webpart, and place the following code in it:
<script language="JavaScript">
_spBodyOnLoadFunctionNames.push("PrepareLinks");

function PrepareLinks()
{
  //create an array to store all the anchor elements in the page 
   var anchors = document.getElementsByTagName("a");
  //loop through the array
  
  for (var x=0; x<anchors.length; x++) 
   {
      //does this anchor element contain #openinnewwindow?
       if (anchors[x].outerHTML.indexOf('#openinnewwindow')>0)
        {
           //store the HTML for this anchor element
           oldText = anchors[x].outerHTML;
           //rewrite the URL to remove our test text and add a target instead
           newText = oldText.replace(/#openinnewwindow/,'" target="_blank');
           //write the HTML back to the browser
           anchors[x].outerHTML = newText;
         }
     }
 }
</script>

If you want all of your external links to open in the new window you can make use of the below code:
<script>
 
var thisDomain = window.location.hostname;
var theLinks = document.links;
 
 for (i=0; i < theLinks.length; i++)
   {
    var thisLink = theLinks(i);
   if (thisLink.href.indexOf(thisDomain) == -1 && thisLink.href.indexOf("javascript") == -1)
     {
       thisLink.target = "_blank";
      //alert(thisLink);
     }
   }
</script>

Or You can use the jQuery Equivalent to make SharePoint 2010 link list open in a new window:
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>

<script>
$(function() {
     $('a').filter(function() {
          return this.hostname && this.hostname !== location.hostname;
          }).attr('target','_blank');
});
</script>

Similarly, To open SharePoint Top Navigation Links in a New window, Refer: SharePoint Top Navigation Link: Open in New Window



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

18 comments :

  1. Great help! Thank you. I've added your blog to my RSS feeder for more. -Arnie

    ReplyDelete
  2. THank you so much,,,

    ReplyDelete
  3. Been looking at loads of long winded work arounds for this solution but this worked first time without any issues!! Thank You!!!

    ReplyDelete
  4. Running 2012 - not working for me- anything I should check? I checked the code and it still opens in the same window for me.

    ReplyDelete
  5. Thanks! This was very helpful.

    ReplyDelete
  6. Excellent solutions! Thanks so much for sharing them.

    ReplyDelete
  7. Great no code solution in option 1. Thank you!!

    ReplyDelete
  8. This is a way better solution than any of the others I have been searching. Thank you very much!

    ReplyDelete
  9. Thanks for this. I used solution 5. Works great for my purposes - at least until I change the view of my list to groupings. My links no longer open in a new window until I remove the group view. How can I fix this?

    ReplyDelete
  10. Thanks! Great post!
    I chose solution 5 for all external links and recommend adding the jQuery lib to a document library and referencing to it.

    ReplyDelete
  11. It Works..... Thanks for this great solution!!!

    ReplyDelete
  12. Thank you!!! Not only have you provided a simple and direct answer, but, your site looks like one to review regularly... please keep posting regularly ;-)

    ReplyDelete
  13. Thanks for the preparelinks function. Is there a way to style the window, i.e. 'toolbar=no,location=no,directories=no, status=no,menubar=no,scrollbars=no,resizable=yes,locationbar=no, titlebar=no, width=400,height=200,left=200,top=50');
    thanks.

    ReplyDelete
  14. Thank you so much! It's really great solution!!!

    ReplyDelete
  15. Hello, thanks a lot for article. I used code for "openinnewwindow" in SP2013, it works but when I use Item limit with "Display items in batches of the specified size", so it works only at first view:( Other solutions to open every all links in new window don´t work:( What can I do?

    ReplyDelete
  16. Really Great post and works great in SharePoint 2013

    ReplyDelete
  17. Only works on the first page ie. on the page that shows the first 30 items

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...