Title: Adding target_&#8221;blank&#8221; to all links (with javascript)
Last modified: January 27, 2019

---

# Adding target_”blank” to all links (with javascript)

 *  [Jack Sarlo](https://wordpress.org/support/users/redhalls/)
 * (@redhalls)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/)
 * Hi,
 * Someone told me I can use the javascript below to convert all external links 
   to open in new tab.
 *     ```
       jQuery(document.links) .filter(function() { return this.hostname != window.location.hostname; }) .attr('target', '_blank'); 
   
       Read more: https://html.com/attributes/a-target/#ixzz5dpkM3QDM
       ```
   
 * Where do I put the code? In the header part? In the footer?
 * I have a plugin that allows me to put code in those parts (and in before/after
   post)
 * Thanks

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11136460)
 * Why not use an existing plugin?
 * [https://wordpress.org/plugins/search/external+links/](https://wordpress.org/plugins/search/external+links/)
 *  Thread Starter [Jack Sarlo](https://wordpress.org/support/users/redhalls/)
 * (@redhalls)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11136506)
 * Someone suggested it’s a good idea to limit the number of plugins we use.
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11136520)
 * “someone suggested”, yet you’re looking for a plugin to let you insert jQuery.
   🙂
 * Go with the easy solution.
 *  Thread Starter [Jack Sarlo](https://wordpress.org/support/users/redhalls/)
 * (@redhalls)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11136544)
 * I thought I could just put the javascript in the header, that would be easy.
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11136684)
 * Edit your theme’s header.php but be advised that any changes you make to the 
   theme will be lost when/if it’s updated, so you’re best advised to use a child
   theme or make your own plugin to add it to the header or use one of the plugins
   that lets you add code to the header. The example you showed above lacks the 
   necessary script tags to be inserted.
 *  Thread Starter [Jack Sarlo](https://wordpress.org/support/users/redhalls/)
 * (@redhalls)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11140190)
 * ok, I can put the following between the <head> </head> tags.
 * `<script>jQuery(document.links) .filter(function() { return this.hostname != 
   window.location.hostname; }) .attr('target', '_blank');</script>`
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11140215)
 * I’m not sure if the header is the right place for that because it may execute
   before the page is entirely loaded. It might work better in the footer.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Adding target_”blank” to all links (with javascript)’ is closed to new
replies.

## Tags

 * [external links](https://wordpress.org/support/topic-tag/external-links/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 2 participants
 * Last reply from: [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * Last activity: [7 years, 3 months ago](https://wordpress.org/support/topic/adding-target_blank-to-all-links-with-javascript/#post-11140215)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
