Skip to content

Instantly share code, notes, and snippets.

@ScarletPonytail
Last active April 17, 2019 15:16
Show Gist options
  • Save ScarletPonytail/6ab1d59f8873a7c75f77a2bc4249c08f to your computer and use it in GitHub Desktop.
Save ScarletPonytail/6ab1d59f8873a7c75f77a2bc4249c08f to your computer and use it in GitHub Desktop.
/*
* Add title attribute to file links to indicate that it opens in a new window
*/
var file_type = [ 'a[href$="pdf"]', 'a[href$="doc"]', 'a[href$="docx"]', 'a[href$="xls"]', 'a[href$="xlsx"]' ]; // File type options
var file_type_length = file_type.length; // File type on page?
for( var i = 0; i < file_type_length; i++ ) { // Loop through links on the page
$( file_type[ i ] ).each( function() {
var file_type_names = file_type[ i ]; // Identify file type
var file_type_name = file_type_names.split('"'); // Split to get file name from string
if ( $( this ).attr( 'title' ) === undefined || $( this ).attr( 'title' ) === false || $( this ).attr( 'title' ) === '' ) { // Is the title missing or empty
$( this ).attr( 'title', '' + 'Opens in a new window' + ', ' + file_type_name[1] );
}
else { // Append existing title
$( this ).attr( 'title', $( this ).attr( 'title' ) + ' - Opens in a new window' + ', ' + file_type_name[1] );
}
});
}
for( var j = 0; j < file_type_length; j++ ) {
$( file_type[ j ] ).each( function() {
if ( $( this ).attr( 'target' ) === undefined || $( this ).attr( 'target' ) === false || $( this ).attr( 'target' ) === '' ) { // Is the target missing or empty
$( this ).attr( 'target', '' + '_blank' );
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment