Javascript - Rediect all outside links to new tab


This script will redirect all links that are pointing outside your site to a new tab, by appending blank to all of them. Insert into a script editor web part, or your master page.

<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(){

// Prevent conflict with other JQuery
 var $nw = jQuery.noConflict();
// Capture host location path
 var url = '://' + window.location.hostname;

// Lowercase everything to compare
 url = url.toLowerCase();
 $nw("a").each(function() {
// Assign the link object to another variable for easier manageability
 var link = this;
// Lower case it
 var linkHref = link.href.toLowerCase();
// MOST IMPORTANT check to see if this A object has this domain in it and make sure it's not a javascript call
 if(linkHref.indexOf(url) < 0 && linkHref.indexOf('javascript:') < 0){
// Change the target to be in the new window
 link.target = '_blank';
 }
 });
});
</script>

Source:
https://social.technet.microsoft.com/Forums/ie/en-US/e535842f-790f-4acd-84a7-787f540a4501/how-to-set-individual-managed-metadata-navigation-links-to-open-in-new-window?forum=sharepointgeneral
Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment