beta beta - 3 months ago 10
HTML Question

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

On my page http://verein-iks.eu/dev/ 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

target='_blank'
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
target="_blank"
. Strangely, when I inspect the element (in Chrome via Ctrl+Shift+I), the
target="_blank"
property seems to have disappeared.

Answer

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

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

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

    $('input,a,select,textarea,button').removeAttr('tabindex');

}(jQuery));
//]]>
</script>

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.