SharePoint: Defaulting external links to open in a new window

Within SharePoint, you can have links to other resources outside of SharePoint. These could go to other internal systems, or external websites. Also, if you’re using Enterprise Search and index websites which are not within SharePoint the search results will include links to outside of SharePoint. The issue with linking to external sites and resources outside of SharePoint is that the link takes over the window so the user leaves SharePoint. Instead, I want the user to go to the link within a new window, thereby keeping the user in SharePoint when they’re done with other resource.

Pretty simple concept, but updating links manually throughout SharePoint might be a pain. Below is a simple little JQuery script that checks all hyperlinks on the page and if it references an external URL, it will set the target to blank! Throw this in your masterpage or in a content editor web part if you only want it on a couple of pages.

NOTE: you’ll need to include a reference to the JQuery files, below I’m using Google’s API, but I recommend uploading the JQuery file into your farm and referencing that instead.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 var url = '://' + window.location.hostname;

 // get the current website name, and i add :// to make sure we're looking at the right name
 // in the URL, rules out http://www.google.com/site:wss.domain.com
 url = url.toLowerCase(); // lowercase everything to compare apples to apples
 $("a").each(function() {
 var link = this; // assign the link object to another variable for easier managability
 var linkHref = link.href.toLowerCase(); // lower case it
 if(linkHref.indexOf(url) < 0 && linkHref.indexOf('javascript:') < 0){
 // check to see if this A object has this domain in it and make sure it's not a javascript call
 link.target = '_blank'; // change the target to be in the new window
 }

 if(linkHref.indexOf('.pdf') > 0){ // check to see if this is a PDF
 link.target = '_blank'; // change the target to be in the new window
 $(link).removeAttr("onclick"); //remove the SP click event
 }

 if(linkHref.indexOf('/forms/') > 0 && linkHref.indexOf(').aspx') > 0){
 //check for links in the forms library
 link.target = '_blank'; // change the target to be in the new window
 $(link).removeAttr("onclick"); //remove the SP click event
 }
 });
});
</script>

23 thoughts on “SharePoint: Defaulting external links to open in a new window

Add yours

  1. Hi, I’m trying to get this to work for the Link to Document content types from a document library web part and it’s not working – any ideas? Thanks Much

  2. Well originally i thought i was having the same issues as Ralph but now i dont think my script is firing. I am not sure what the best way is to implement this script on my list. I guess my question would be can this script execute on hyperlinks that are inside a Links Web Part? If so what would be the best way to implement it so that it does that on Hyperlinks in my list?
    I have tried so many different ways and scripts, and none of them seem to work.

  3. In debugger, I only find that in the javascript…On the links themselves. this one being MSN, I find this, this was in summary view and the little script works, as soon as I group it, it fails…. :

    MSN 

  4. Oh, what I was saying is your code works great for external sites in a list, seems to work no mater what… it works as you intended… I would like to have a new window for any type of link even when they are grouped..Some examples I’ve seen don’t work on a grouped list, but I don’t know why..

    https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js

    $(document).ready(function(){
    $(“.ms-vb a”).attr(‘target’, ‘_blank’);
    });

    This works great except as soon as I group it…The functionality is lost.

  5. Hi David! This works great!

    Sorry to be a bother, but I was wondering if you had something like this for internal and external sites and that would work on a grouped list..Some scripts I’ve tried won’t work with a grouped list..

      1. Both SharePoint and External…Basically anything. I am making a popup links list…That will give people a common set of links…The striped down popup window will show the list and will display on logon. So anything they click on produces a separate window.

        1. This code is grabbing all a tags, all links. If the links are using JS to open links that me be interfering. Can you point me to an external reference?

  6. Abu – with the hyperlink- publishing type column you lose sorting. Column head freezes. To David! – thank you for your post it was the most simple and straight to the point I’ve seen across many pages! Kudos.

    1. Hi David,

      This works great! I was just wondering if you had something that works for both internal and external sites, your code worked great in a grouped list, which is what I’m using.

  7. Thanks! But simple and easiest solution is: Create a Site column of “Hyperlink with formatting and constraints for publishing” type and add it in your list, which enables you to specify “Open in New Window” Option!

    There are few more possible solutions, including JavaScript, JQuery, SharePoint Designer, List Schema Edit to make SharePoint Link list open in new window at SharePointDiary.com –

    SharePoint Link list: Open in a New Window

    1. True, but don’t you need publishing enabled? And that only pertains to hyperlink fields, what about other links, in content, in navigation, etc.? A single solution to ensure all external links open in a new window would include JavaScript/jQuery, as you noted, and as I posted ;)

  8. Use the JavaScript function to open a new window in SharePoint 2010.

    Create the function to open your target window as sample provide below.

    function load_url(externallink)
    {

    window.open(externallink,target=’_blank’)

    }

    Place the function load_url in JavaScript file

    Click site actions, select manage content and structure.
    Suppose you want to change the links in the page
    http://someserver/sites/Dev/Help/HelpLinks/AllItems.aspx

    Then select the List named HelpLinks in the sub site Help. Dev will be the top most node(site). Help will be a sub site and inside Help you can find List by name HelpLinks.

    1. All the links in the page and their title will be displayed
    2. Select the title of link which you want to open in new tab and right click.
    3. Select Edit properties. Then in the URL field and call the function as javascript:load_url(‘http://www.google.co.in’); instead of http://www.google.co.in

    Or Else
    Suppose you want to change the links in the below URL.
    URL: http://someserver/sites/Dev/Help/HelpLinks/AllItems.aspx

    Go To
    1. open to the link http://someserver/sites/Dev/Help/HelpLinks/AllItems.aspx
    2. You will find the columns of the List (Title Column, URL column, Summary etc).
    3. Select the Title and click Edit property which you want to edit
    4. Then in the URL field and call the function as javascript:load_url(‘http://www.google.co.in’); instead of http://www.google.co.in

Leave a Reply

Up ↑

Discover more from David Lozzi

Subscribe now to keep reading and get access to the full archive.

Continue reading