Target _blank in all Link
I've just made an html page with target _self
But now I have too many links and I want to change all link targets to _blank
and it's hard for me to do. Is there is any javascript which apply on all just to write 1 time ??
Because my code is too long and it is taking too many times to change in all links. Is there is any trick?
like this
<a href="http://www.google.com"></a>
<a href="http://www.facebook.com"></a>
<a href="http://www.gmail.com"></a>
<a href="http://www.twitter.com"></a>
<a href="http://www.stackoverflow.com"></a>
<a href="http://plus.google.com"></a>
Put this in your <head>
:
<base target="_blank">
It will make all URLs on a page open in a new page, unless target
is specified.
This is a HTML5-only feature, I learned it from Google's io-2012-slides slide package.
To answer your question, jQuery makes this easy:
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(function() {
$('a[href]').attr('target', '_blank');
});
</script>
This will not modify any <a>
tags without an href
attribute.