I've got a lot of links (150+) in an href
(here I can only post 2) and I want them to be opened in a new tab or window. I've got this piece of code that works, but before I add it to my website and before I add it to a lot of articles of mine (250+), I want to make sure the syntax of the code is 100% correct. Also, do I need the <p>
?
<script>
window.onload = function(){
var a = document.getElementById('links').getElementsByTagName('a');
for (var i=0; i<a.length; i++){
a[i].setAttribute('target', '_blank');
}
}
</script>
<div id="links">
<p><a href="http://www.google.com/" >test</a></p>
<p><a href="http://www.google.com/" >test</a></p>
</div>
<p>
tag. If u want to separate them to two lines, then keep it \$\endgroup\$target="_blank"
to your links in your source code (html/php files or in the DB) rather than using a client-side script (it adds to the load, some users might have JS disabled and in general it's not elegant). But if you have to do it this way, it's fine.<p>
's are not neccessary as others have said, but you also don't need to use them for the layout and neither you have to add <br> to separate them. You can use CSS#links a { display: block; }
instead. \$\endgroup\$