The links list web part is a quick and easy to add a list of links to vendors, other SharePoint sites or the most popular documents in a library. The problem is that the links list web part does not have an “Open in new window” option. So when your users click the list, they navigate away from your site.
A typical links list:
To change a links list to force “Open in new window”:
- Add a Content Editor Web Part (CEWP) just below the links list web part
- Modify the CEWP and set a title then click Source Editor
- Copy and paste the HTML and JavaScript below
- Edit the JavaScript to change the word “Links” to the name of your links list web part (title of the web part, not the title of the actual list, although they may be the same)
if (x(i).summary=="Links") to
if (x(i).summary=="Your Links List Title")
(If you are not sure, display your page, select Source from the View menu and search for “summary”) - Also… in the Advanced section of the CEWP’s properties change Chrome to “None” to hide the CEWP
- Exit the edit mode and see if it works
<script> // CEWP trick from techtrainingnotes.blogspot.com! // Find the link list (change "Links" to your web part's name) var x = document.getElementsByTagName("TABLE") // find all of the Tables var LinkList var i=0; for (i=0;i<x.length;i++) { if (x[i].summary=="Links") { // LinkList = x[i]; break; } } // add a target to the <A> tags var links = LinkList.getElementsByTagName("A") // find all of the for (i=0;i<links.length;i++) { links[i].target="_blank" } </script>
.
14 comments:
I went through 20 different Java Scripts left by other people. None of them worked until yours. It worked like a charm! Thanks.
Excellent! One further question...I have a document library where we've enabled the "Link to Document" content type in addition to the default. Is there an additional IF statement that can be added to only attach the target for items of type "Link to Document"?
JeffM,
Probably not. Link to Document is actually a link to as ASPX page stored in the library that redirects to the target when it loaded.
Mike
Hi,
I am wondering if i need to add anything to my site, because for some reason this is not working for me.
Javascript is supposed to run out of the box, right?
Anon,
> Javascript is supposed to run out of the box, right?
Yes. The biggest issues with JavaScript are capitaliztion and placement. Any typo in your code will cause it to fail, so double check all of it.
Make sure the Content Editor Web Part is placed below the Link List web part.
If you are using SP 2010 then see this article about JavaScript and the Content Editor Web Part:
http://techtrainingnotes.blogspot.com/2012/05/adding-javascript-and-css-to-sharepoint.html
Mike
Thank you Mike for the write-up!
I just wanted you to know I tried this. I'm using SP2010 so I'm not sure if that is why there is some difference. Using the "links list web part name" did not work for me. I searched the source for "summary" and found it referencing the Link List itself, by name. I used that instead and it works. One thing I noticed, SP added a space at the end of the links list name in the source. Example: My links list is named "links_resources", sharepoint modified it for some reason to "links_resources ". The JS would not fire without me adding the space on the end.
Tony
Tony,
Should work the same in 2010. And yes, 2010 does add that extra space as a gap between the title and the optional description, even if there is no description.
Mike
Thank you for the reply Mike. I've gone back and tried using the links list web parts name I had specified manually (Resources), but it still would not fire correctly (opens the links in same window). Changing it back to the list's actual name (links_resources < and adding the space on the end), it fires right.
Tony
I have tried to add the code to my SP2010 Standard MasterPage, but the JavaScript code gives off many errors. I added a semicolon to each variable declaration. X does not seem to be populating anything.
I have tried to add the code to my SP2010 Standard MasterPage, but the JavaScript code gives off many errors. I added a semicolon to each variable declaration. X does not seem to be populating anything.
Anonymous,
Two common errors...
1) Make sure when adding the JavaScript to the page that it is somewhere after where the web part will be loaded. A good place is just before the </body> tag.
2) 2010 often adds an extra space or a full description after the list name in the summary attribute.
I.e. summary="Links " (extra space)
or
summary="Links Add your links here"
View the source of the page and copy and paste the full "summary" text.
Mike
Had the same problem as anonymous, but now it works. Thanks for this script and page. I have spent two days trying to figure this out, and now I can finish the project.
-Ryan
Thanks a lot for the script. Works great after adding the extra space.
Thanks! Its works for me. The document opened in new tab but the original page also opened the document. How can I open the document in the new tab only?
Post a Comment
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.