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=""></script>
<script type="text/javascript">

// 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 = '_blank';

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


Post a Comment

Note: Only a member of this blog may post a comment.