beta beta - 1 year ago 70
HTML Question

WordPress opens link in same window despite target="_blank"

On my page I have a Facebook button on the top-right. If I click the button, I get forwarded to a Facebook page. However, the link is opened in the same window. I want it to be opened in a new window/tab. Hence, I use

in the HTML code. Still, the link gets opened in the same window! Why?

When checking the source code (Ctrl+U in Firefox and Chrome) of the page, you can see on line 166 that the target is indeed set to
. Strangely, when I inspect the element (in Chrome via Ctrl+Shift+I), the
property seems to have disappeared.

Answer Source

In the footer of your DOM, there is a <script> that is removing the target attribute from all links...

<script type="text/javascript">
(function( $ ) { 'use strict';

    $('a').removeAttr('target');    //This will remove the target attribute from all links on page load   



The reason you see the attribute when viewing the source is due to the fact that the source is what was received from the server. Inspect Element is a live view of what happens with the DOM, so it renders the result of the executed JS.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download