Let's say I have the following code:
<div id="link_other"> <ul> <li><a href="http://www.google.com/">google</a></li> <li> <div class="some_class"> dsalkfnm sladkfm <a href="http://www.yahoo.com/">yahoo</a> </div> </li> </ul> </div>
In this case, the JavaScript would add target="_blank"
to all links within the div link_other
.
How can I do that using JavaScript?
Conclusion. You can use the target="_blank" attribute if you want your users to click on a link that opens up a new browser tab. The target="_blank" attribute is used inside the opening anchor tag like this.
target="_blank" is a special keyword that will open links in a new tab every time. target="blank" will open the first-clicked link in a new tab, but any future links that share target="blank" will open in that same newly-opened tab.
Unfortunately, no. In 2013, there is no way to do it with pure CSS.
To change the target of a link in HTML, use the target attribute of the <a>… </a> tag. The target attribute can be used to open any link in a new tab, or the same tab, etc. Opens the linked page in a new tab.
HTML target=”_blank” Link Tutorial with Examples 1 HTML Link with <a> Element. The HTML <a> link element with target="_blank" is supported by all major modern web browsers like Google Chrome, Microsoft Edge, Internet Explorer, Mozilla Firefox, Safari, ... 2 target=”_blank” vs target=”_new”. ... 3 Secure target=”_blank”. ...
HTML Link with <a> Element The HTML <a> link element with target="_blank" is supported by all major modern web browsers like Google Chrome, Microsoft Edge, Internet Explorer, Mozilla Firefox, Safari, and Opera. The syntax of the target="_blank" is like below.
HTML provides the <a> element or tag in order to create links to the other pages, URL, or part of the same page. While creating a link we can set the style of the link like open in the same browser tab or page or in a new browser window or tab.
According to standards like HTML, HTML 5, HTML 4 etc there is no attribute like target="_new". But modern browsers provide applications about the target="_new" even not a standard.
/* here are two different ways to do this */ //using jquery: $(document).ready(function(){ $('#link_other a').attr('target', '_blank'); }); // not using jquery window.onload = function(){ var anchors = document.getElementById('link_other').getElementsByTagName('a'); for (var i=0; i<anchors.length; i++){ anchors[i].setAttribute('target', '_blank'); } } // jquery is prettier. :-)
You could also add a title tag to notify the user that you are doing this, to warn them, because as has been pointed out, it's not what users expect:
$('#link_other a').attr('target', '_blank').attr('title','This link will open in a new window.');
Non-jquery:
// Very old browsers // var linkList = document.getElementById('link_other').getElementsByTagName('a'); // New browsers (IE8+) var linkList = document.querySelectorAll('#link_other a'); for(var i in linkList){ linkList[i].setAttribute('target', '_blank'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With